-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (80 loc) · 2.51 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var express = require('express');
var app = express();
var path = require('path');
var mongo = require("mongodb").MongoClient;
var urlRegex = require('url-regex');
var rg = require('rangen');
var validUrl = require('valid-url');
var dburl = process.env.dburl;
var myURL, checkURL, shortURL;
app.use('/',express.static('public'));
app.get('/new/:url(*)',function(req,res){
myURL = req.params.url;
if (validUrl.isUri(myURL)){
mongo.connect(dburl, function (err, db) {
if (err) {
res.end('Unable to connect to the mongoDB server. Error:');
return console.log(err);
} else {
console.log('Connection established to', dburl);
var urlList = db.collection('urlList');
var short = rg.id(2, 'n');
urlList.insert([{url: myURL, short: short}],function(){
var data = {
original_url: myURL,
short_url: 'http://'+req.headers['host']+'/'+short
}
res.send(data);
});
db.close();
}
});
} else {
res.json({
original: myURL,
error: "is not a URL"
});
}
});
app.get('/:id',function(req,res){
var id = req.params.id;
mongo.connect(dburl, function (err, db) {
if (err) {
res.end('Unable to connect to the mongoDB server. Error:');
return console.log(err);
} else {
console.log('Connection established to', dburl);
var urlList = db.collection('urlList');
urlList.find({short:id}).toArray(function(err,docs){
if(err){
res.end('not found')
return console.log('read',err);
} else {
if(docs.length>0){
db.close();
res.redirect(docs[0].url);
} else {
db.close();
res.end('error')
}
}
})
}
});
});
app.get('/', function(req, res) {
var fileName = path.join(__dirname, '/views/index.html');
res.sendFile(fileName, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', fileName);
}
});
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});