-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (93 loc) · 3.76 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
var express = require("express");
var path = require("path");
var base51 = require("./base51.js");
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// DB Connection URL
var url = process.env.MONGOLAB_URI;
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
/* Routs
--------------------
*/
app.get("/", function(req, res){
res.sendFile(path.join(__dirname, "/index.html"));
});
app.get("/new/", function(req, res){
res.send("Add a url to use the shortener function");
});
app.get("/:data", function(req, res){
var data = req.params.data;
console.log("/:data Received: " + req.params.data);
if(data.length < 10){ // All safe integers fit into 10 encoded characters
MongoClient.connect(url, function(err, db){
assert.equal(null, err);
var urls = db.collection("urls");
urls.findOne({"_id": base51.decode(data)}, {fields: {"original_url": 1}}, function(err, doc){
assert.equal(null, err);
console.log(doc);
db.close();
if(doc){
res.writeHead(301, {Location: doc.original_url});
res.end();
}else{
res.writeHead(404, {"Content-Type": "application/json"});
res.end(JSON.stringify({"error": "404 This url is not in the database"}));
}
});
});
}else{
res.writeHead(400, {"Content-Type": "application/json"});
res.end(JSON.stringify({"error": "400 Url parameter is too long"}));
}
});
app.get("/new/*", function(req, res){
var data = req.url.substring(5);
console.log("/new/:data Received: " + data);
console.log("url: ", req.url);
console.log("host: ", req.get("host"));
// Check if valid url
var matches = data.match(/((http(s)?:\/\/(?:www\.)?[A-Za-z0-9\.\-\:]+)(\/.*)?)/);
if(matches && matches[0] === data){
var response;
// Connect to db
MongoClient.connect(url, function(err, db){
assert.equal(null, err);
var counters = db.collection('counters'),
urls = db.collection("urls");
getNextSequenceValue(counters, "urlid", function(nextId){
console.log("next id: ", nextId);
// Build response
response = {"original_url": data, "short_url": ("https://" + req.get("host") + "/" + base51.encode(nextId))};
console.log(response);
// Store urls in db
urls.insert({
"_id": nextId,
"original_url": response["original_url"],
"short_url": response["short_url"]
}, function(err, result){
assert.equal(err, null);
db.close();
// Send response
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(response));
})
});
});
}else{
res.writeHead(400, {"Content-Type": "application/json"});
res.end(JSON.stringify({"error": "400 Bad url format, make sure you are using http/https and a real site"}));
}
});
app.listen(process.env.PORT || 8080);
/* Increase a sequence value by one and returns the new value */
function getNextSequenceValue(collection, sequenceName, callback){
collection.findAndModify({_id: sequenceName },
[["_id", 1]],
{$inc:{"sequence_value":1}},
{new: true},
function(err, result){
assert.equal(err, null);
callback(result.value.sequence_value);
});
}