-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
86 lines (67 loc) · 2.37 KB
/
server.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
// ./mongod and mongo to run
//https://forum.freecodecamp.com/t/guide-for-using-mongodb-and-deploying-to-heroku/19347
var express = require('express');
var app = express();
var path = require('path');
var mongoose = require('mongoose');
var PostSchema = mongoose.Schema({
_id: { type: Number },
longURL: { type: String }
});
var Post = mongoose.model('Post', PostSchema);
var num;
//var url = 'mongodb://localhost/test';
console.log('mongolab_uri ' + process.env.MONGOLAB_URI);
var url = process.env.MONGOLAB_URI;
mongoose.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
}
});
app.set('port', (process.env.PORT || 5000));
app.get('/', function(req,res) {
res.sendFile(path.join(__dirname+'/index.html'));
});
app.get('/*', function(req, res){
var input = req.params[0];
if (input.length < 5 && input < 1000) { // this will be short URL action
Post.findOne({ '_id': input }, function (err, post) {
if (err) return console.error(err);
if (post == null) { // if there is no match
res.send('Short URL not in database');
} else {
console.log(post.longURL);
//res.send(post.longURL);
res.redirect(post.longURL);
}
});
} else if (!isUrl(input)){
res.send('Input URL not valid! Please re-enter');
} else {
num = Math.floor(Math.random() * 1000) + 1;
new Post({
_id: num,
longURL: input
}).save(function (err) {
if (err) return console.error(err);
res.send({ "original_URL":input, "short_url":"https://cfurl.herokuapp.com/" + num});
});
/*
Post.find(function (err, posts) {
if (err) return console.error(err);
console.log(posts);
});
*/
}
});
// http://stackoverflow.com/questions/1701898/how-to-detect-whether-a-string-is-in-url-format-using-javascript
function isUrl(s) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
return regexp.test(s);
}
//app.listen(8080);
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});