-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (72 loc) · 2 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
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
/*jslint node: true */
'use strict';
// Declare variables used
var app, base_url, bodyParser, client, express, port, rtg, shortid;
// Define values
express = require('express');
app = express();
port = process.env.PORT || 5000;
shortid = require('shortid');
bodyParser = require('body-parser');
base_url = process.env.BASE_URL || 'http://localhost:5000';
// Set up connection to Redis
/* istanbul ignore if */
if (process.env.REDISTOGO_URL) {
rtg = require("url").parse(process.env.REDISTOGO_URL);
client = require("redis").createClient(rtg.port, rtg.hostname);
client.auth(rtg.auth.split(":")[1]);
} else {
client = require('redis').createClient();
}
// Set up templating
app.set('views', __dirname + '/views');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
// Set URL
app.set('base_url', base_url);
// Handle POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Define index route
app.get('/', function (req, res) {
res.render('index');
});
// Define submit route
app.post('/', function (req, res) {
// Declare variables
var url, id;
// Get URL
url = req.body.url;
// Create a hashed short version
id = shortid.generate();
// Store them in Redis
client.set(id, url, function () {
// Display the response
res.render('output', { id: id, base_url: base_url });
});
});
// Define link route
app.route('/:id').all(function (req, res) {
// Get ID
var id = req.params.id.trim();
// Look up the URL
client.get(id, function (err, reply) {
if (!err && reply) {
// Redirect user to it
res.status(301);
res.set('Location', reply);
res.send();
} else {
// Confirm no such link in database
res.status(404);
res.render('error');
}
});
});
// Serve static files
app.use(express.static(__dirname + '/static'));
// Listen
app.listen(port);
console.log('Listening on port ' + port);