-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (42 loc) · 1.04 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
var express = require('express');
var app = express();
var redis = require("redis"),
client = redis.createClient();
client.on('connect', function() {
console.log('Redis connected');
});
var shortenerUrl = 'whatever the url is for this site';
app.get('/:link', function(req, res) {
if (!req.params) {
res.send('You need specify a link');
}
var link = req.params.link;
client.get(link, function(err, reply) {
if (err) {
res.send('There was an error')
}
if (!reply) {
res.send('Could not find that link');
}
var url = [shortenerUrl, reply].join('');
res.redirect(url);
});
});
app.post('/:link', function(req, res) {
if (!req.params) {
res.send('You need specify a link');
}
var link = req.params.link;
client.get(link, function(err, reply) {
if (err) {
res.send('There was an error')
}
if (!reply) {
client.set(link, shortenerUrl);
res.redirect('/')
}
var url = [shortenerUrl, reply].join('');
res.redirect(url);
});
});
app.listen(3000);