-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathapp.js
60 lines (53 loc) · 1.55 KB
/
app.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 express = require('express'),
_ = require('underscore'),
cons = require('consolidate'),
analytics = require('./analytics'),
redirects = require('./redirects.json'),
app = express()
;
app.locals._ = _;
var startApp = function(cb) {
// Set up our views
app.engine('html', cons.ejs);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.get('/', function(req,res){
res.render('index', {
error: false,
redirects: redirects
});
});
app.get('/robots.txt', function(req, res) {
res.set('Content-Type', 'text/plain');
res.send('User-agent: *\nDisallow: /\n');
});
app.get('/:wanted', function(req,res){
if (typeof(redirects[req.params.wanted]) === 'undefined') {
// I don't know that link
var request_url = req.protocol + '://' + req.get('host') + req.originalUrl;
res.render('index', {
error: true,
request_url: request_url
});
} else {
analytics.trackRedirect(req);
// Send them to the right place
res.redirect(302, encodeURI(redirects[req.params.wanted]));
}
});
app.get('/.well-known/status', function(req,res){
res.json({
status: "ok",
updated: Math.floor(new Date() / 1000),
dependencies: [],
resources: {}
});
});
var server = app.listen(process.env.PORT || 3000, function(){
var host = server.address().address;
var port = server.address().port;
console.log('App listening at port %s', port);
cb(null,host,port,this);
});
}
module.exports = startApp;