-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.js
61 lines (42 loc) · 1.04 KB
/
operations.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
var links = {};
exports.get = function (link) {
if (link.data) {
var url = links[link.data];
if (url) {
link.send(200, url);
return;
}
link.send(200, "Invalid short url.");
return;
}
link.send(200, "No data sent to me.");
}
exports.insert = function (link) {
// TODO: validation
if (link.data) {
var url = generate(4);
while (links[url]) {
url = generate(4);
}
links[url] = link.data;
link.send(200, url);
return;
}
link.send(200, "No data sent to me.");
}
exports.delete = function (link) {
if (link.data === "deleteAll") {
links = {};
link.send(200);
return;
}
link.send(200, "Invalid password");
}
function generate(l) {
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYXabcdefghijklmnopqrstuvwxyz";
var res = "";
for (var i = 0; i < l; i++) {
res += letters[Math.floor(Math.random() * letters.length)];
}
return res;
}