-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
120 lines (105 loc) · 3.09 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var express = require('express');
var path = require('path');
var url = require('url');
var mongoclient = require('mongodb').MongoClient;
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.get(/^\/new\/([:\w.\/]*)/, function(req, res) {
console.log('got to our special place: ' + req.params[0]);
//parse, validate, and test url
var oldURL = url.parse(req.params[0]);
var h;
console.log(oldURL.protocol);
switch (oldURL.protocol) {
case 'http:':
h = require('http');
break;
case 'https:':
h = require('https');
break;
default:
showJSONerror(res);
return;
}
console.log('stored ' + h);
var options = {
protocol: oldURL['protocol'],
hostname: oldURL['hostname'],
method: 'HEAD'
};
var pingreq = h.request(options, function(pingres) {
console.log('we made a ping: ' + pingres.statusCode);
if (pingres.statusCode === 200) {
mongoclient.connect('mongodb://localhost:27017/appdata', function(err, db){
if (err) throw err;
console.log('opened connection to mongo');
//add one to the counter
db.collection('nextNum').update({}, {
$inc: {counter: 1}
});
console.log('incremented db?');
//get the new counter value
db.collection('nextNum').find({}, {counter:1, _id:0}).toArray( function(err, data){
if (err) throw err;
console.log('inside fn promise, current val: ' + data[0].counter);
//make new URL
var count = data[0].counter;
var newURL = 'https://shorten-url-hogdogthegod.c9users.io/' + count;
var newObj = {
'original-url': oldURL.href,
'short-url': newURL
};
console.log('created object:');
console.log(newObj);
//output info
res.json(newObj);
console.log('returned json');
//save to DB
newObj['short-id'] = count;
db.collection('urls').insert(newObj);
console.log('saved to db');
//clean up?
res.end();
db.close();
console.log('cleaned up');
});
})
} else {
showJSONerror(res);
}
});
pingreq.on('error', function() {
showJSONerror(res)
});
pingreq.end();
});
function showJSONerror(res) {
console.log('caught error');
res.json({'error': 'no such url'});
res.end();
}
app.get('/:id', function(req, res){
console.log('retrieving long url');
mongoclient.connect('mongodb://localhost:27017/appdata', function(err, db) {
if (err) throw err;
var prom = db.collection('urls').find({
'short-id': +req.params.id
}).toArray();
prom.then(function(data) {
if (data.length < 1) {
res.json({'error':'This url is not in the database'});
} else {
res.redirect(data[0]['original-url']);
}
//clean-up
res.end();
db.close();
})
})
});
app.get('*', function(req, res){
showJSONerror(res);
})
app.listen(8080, function() {
console.log('listener added');
})