-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
181 lines (138 loc) · 4.32 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require('dotenv').config({silent: true});
var db = require('mongodb');
db.connect({
url: process.env.DB_URL,
appUrl: process.env.APP_URL,
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
});
// URL Shortener
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var config = require('./config');
var base58 = require('./base58.js');
var Url = require('./models/url');
// built following the tutorial @
// https://coligo.io/create-url-shortener-with-node-express-mongo/
// Working to meet user stories @
// https://little-url.herokuapp.com/
// create a connection to our MongoDB
mongoose.connect(process.env.DB_URL);
mongoose.Promise = global.Promise;
// for c9 use : 'mongodb://' + config.db.host + '/' + config.db.name
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// tell Express to serve files from public folder
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function(req, res){
// route to serve the homepage
res.sendFile(path.join(__dirname, 'index.html'));
});
// Alt FCC basejump api method for address only submission
function validate(given, err){
given = given.split('');
var taken = [];
for (var i = 0; i < 8; i++ ){
taken.push(given[i]);
}
taken = taken.join('');
if (/https?:\/\//.test(taken)) {
console.log("Valid URL");
} else {
console.log("Invalid URL");
throw ('URL Must have "http://" or "https://" when using the address bar method' + err);
}
}
app.get('/new/:url*', function findNew(req, res, longUrl){
if (req.params['url'] + req.params[0]){
longUrl = req.params['url'] + req.params[0];
}
validate(longUrl);
console.log('found: ' + longUrl);
var shortUrl = '';
// check if url already exists in database
Url.findOne({long_url: longUrl}, function (err, doc){
if (err){
console.log(err);
}
if (doc){
shortUrl = config.webhost + base58.encode(doc._id);
res.send({'originalUrl': longUrl,'shortUrl': shortUrl});
} else {
// The long URL was not found in the long_url field in our urls
// collection, so we need to create a new entry:
var newUrl = Url({
long_url: longUrl
});
// save the new link
newUrl.save(function(err) {
if (err){
console.log(err);
}
// construct the short URL
shortUrl = config.webhost + base58.encode(newUrl._id);
res.send({'original_url': longUrl,'shortUrl': shortUrl});
console.log('Saved: ' + shortUrl);
});
}
});
});
// Alt POST method for UI via coligo
app.post('/api/shorten', function(req, res){
var longUrl = req.body.url;
var shortUrl = '';
// check if url already exists in database
Url.findOne({long_url: longUrl}, function (err, doc){
if (err){
console.log(err);
}
if (doc){
shortUrl = config.webhost + base58.encode(doc._id);
res.send({'shortUrl': shortUrl});
} else { // long URL not found in long_url field in urls collection
// create a new entry:
var newUrl = Url({
long_url: longUrl
});
// save the new link
newUrl.save(function(err) {
if (err){
console.log(err);
}
// construct the short URL
shortUrl = config.webhost + base58.encode(newUrl._id);
res.send({'shortUrl': shortUrl});
console.log('Saved: ' + shortUrl);
});
}
});
});
// Handle a given shortUrl
app.get('/:encoded_id', function(req, res){
var base58Id = req.params.encoded_id;
var id = base58.decode(base58Id);
// check if url already exists in database
Url.findOne({_id: id}, function (err, doc){
if (err){
console.log(err);
}
if (doc) {
// found an entry in the DB, redirect the user to their destination
res.redirect('http://' + doc.long_url);
console.log('Redirecting to: ' + doc.long_url);
} /* else {
// nothing found, take 'em home
res.redirect(config.webhost);
console.log('Unlisted entry');
} */
});
});
// port: 8080 to run on c9
// port: process.env.PORT for heroku
app.listen(process.env.PORT, function () {
console.log('App open on port ' + process.env.PORT + ':');
});