-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl-shortener.js
112 lines (98 loc) · 3.05 KB
/
url-shortener.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
"use strict";
var express = require("express");
var mongoClient = require("mongodb").MongoClient;
var assert = require("assert");
var hashAlgorithm = require("non-crypto-hash").createHash("murmurhash3");
var validUrl = require("valid-url");
var url = require('url');
require("dotenv").load();
var mongoUrl = process.env.MONGO_URI;
var port = process.env.PORT || 8080;
var baseUrl = process.env.BASE_URL;
var db = null;
var collection = null;
var app = express();
mongoClient.connect(mongoUrl, function (err, database) {
assert.equal(null, err);
db = database;
collection = db.collection("shorturl");
app.listen(port, function () {
console.log("Node.js listening on port " + port + "...");
});
});
var path = process.cwd();
app.use(express.static(path + "/public"));
function sendErrorResponse(res, err) {
res.end(JSON.stringify({ "error": err }));
}
app.get("/new/*", function(req, res) {
function validateUrl(inUrl) {
if (!validUrl.isWebUri(inUrl)) {
return false;
}
var urlParts = url.parse(inUrl);
if (!urlParts.host.includes(".")) {
return false;
}
return true;
}
var matchArray = req._parsedOriginalUrl.path.match(/^\/new\/(.*)$/);
var inputUrl = matchArray[1];
if (!validateUrl(inputUrl)) {
sendErrorResponse(res, "URL Invalid");
return;
}
var id = hashAlgorithm.x86Hash32(inputUrl).substr(0, 4);
// use first 4 bytes for id
// if duplicate id (but url is different), add sub-id (start with 1 and keep
// incrementing it till we have unique id). Sub-id appends to id in form of
// even number of digits hex number.
function sendResponse() {
res.end(JSON.stringify({ "original_url": inputUrl, "short_url": baseUrl + id }));
}
function insertResult(err, result) {
if (err) {
if (err.code == 11000) {
collection.findOne({"_id": id, "url": inputUrl}, function (err, result) {
if (err) {
console.log("Error: " + err);
sendErrorResponse(res, err.errmsg);
}
else if (result) {
sendResponse();
} else {
// modify key and attempt to insert
var baseId = id.substr(0, 4);
var subId = id.substr(4);
var subIdVal = 0;
if (subId != "")
subIdVal = parseInt(subId, 16);
subIdVal++;
subId = (subIdVal).toString(16);
id = baseId + ((subId.length % 2 == 0)? "" : "0") + subId;
collection.insertOne({ "_id": id, "url": inputUrl }, insertResult);
}
});
} else {
sendErrorResponse(res, err.errmsg);
}
} else {
sendResponse();
}
}
collection.insertOne({ "_id": id, "url": inputUrl }, insertResult);
});
app.get("/:id", function(req, res) {
var id = req.params.id;
collection.findOne({"_id": id }, function (err, result) {
if (err) {
console.log("Error: " + err);
sendErrorResponse(res, err.errmsg);
}
else if (result) {
res.redirect(result.url);
} else {
sendErrorResponse(res, "No original url found");
}
});
});