-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
225 lines (127 loc) · 4.13 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict';
var express = require('express');
var mongo = require('mongodb');
var Base62 = require("base62");
var path = require("path");
var bodyParser = require('body-parser');
var dns = require('dns');
var nextId = "1";
var url = process.env.MONGOLAB_URI;
mongo.connect(url, function(err, db) {
if (err != undefined) {
throw err;
}
db.collection("links").find().sort({
_id: -1
}).toArray(function(err, data) {
if (data[0] != undefined) {
nextId = Base62.encode(Base62.decode(data[0]["_id"]) + 1);
}
})
})
var cors = require('cors');
var app = express();
// Basic Configuration
var port = process.env.PORT || 3000;
/** this project needs a db !! **/
// mongoose.connect(process.env.MONGOLAB_URI);
app.use(cors());
/** this project needs to parse POST bodies **/
// you should mount the body-parser here
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use('/public', express.static(process.cwd() + '/public'));
app.get('/', function(req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
app.get("/api/shorturl/:id", function(req, res) {
var id = req.params.id;
mongo.connect(url, function(err, db) {
if (err != undefined) {
throw err;
}
db.collection("links").find({
"_id": id
}).toArray(function(err, data) {
if (err != undefined) {
throw err;
}
if (data[0] == undefined) {
res.send({
"error": "Shortened URL not found in database."
});
res.end();
} else {
var theLink = data[0].link;
res.redirect(theLink);
}
})
});
});
app.post("/api/shorturl/new", function(req, res) {
var uRL = req.body.url;
var comPosition = uRL.search(".com");
var hasHttp = uRL.search("http://");
var beginPosition = 7;
if (hasHttp == -1) {
var hasHttp = uRL.search("https://");
beginPosition++;
}
if (hasHttp == 0 && comPosition != -1) {
dns.lookup(uRL.slice(beginPosition, comPosition + 4), function(err) {
if (err == undefined) {
mongo.connect(url, function(err, db) {
if (err != undefined) {
throw err;
}
function insert() {
db.collection("links").insertOne({
"_id": nextId,
"link": uRL
})
var response = {
"original_url": uRL,
"short_url": nextId
};
res.send(response);
res.end()
nextId = Base62.encode(Base62.decode(nextId) + 1)
}
db.collection("links").find({
"link": uRL
}).toArray(function(err, data) {
if (err != undefined) {
throw err;
}
if (data[0] != undefined) {
var response = {
"original_url": uRL,
"short_url": data[0]["_id"]
};
res.send(response);
res.end();
} else {
insert();
}
})
})
} else {
console.log(err);
res.send({
"error": "Improper URL"
});
res.end();
}
})
} else {
res.send({
"error": "Improper URL"
});
res.end();
}
});
app.listen(port || 3000, function() {
console.log('Node.js listening ...');
});