-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (50 loc) · 1.34 KB
/
index.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
// http-ish
var restify = require('restify');
// level config
var level = require('level');
var uuid = require('node-uuid');
var options = {keyEncoding: 'json', valueEncoding: 'json'};
var db = level(__dirname + '/db', options);
var server = restify.createServer({
name: 'shortlevel',
version: '0.1.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.get('/shorten', function (req, res, next) {
if (req.params.url) {
var url = req.params.url;
var urlId = (+new Date()).toString(36);
db.put(urlId, url, function(err) {
if (err) {
console.log('Error', err);
res.send({error: 'Ohnoez, halp.'});
}
else {
console.log('Shortened ' + urlId + ': ' + url);
res.send({url: server.url + '/' + urlId});
}
});
// res.send({urlId: urlId});
}
else {
res.send({error: 'URL was not specified.'});
}
return next();
});
server.get('/:urlId', function(req, res, next) {
db.get(req.params.urlId, function(err, url) {
if (err) res.send({error: 'Unable to find shortened URL.'});
else {
res.writeHead(301, {
'Location': url
});
res.end();
}
});
return next();
});
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url);
});