-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
133 lines (104 loc) · 3.29 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
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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, util = require('util')
, apps = require('./model/apps')
, vestigingen = require('./model/vestigingen')
, verkeer = require('./model/verkeer')
var app = module.exports = express.createServer();
var verkeerFeedTimeout = 1000*60;
/**
* Configuration
*/
// In production log with a 'short' format.
app.configure('production', function() {
app.use( express.logger() );
});
// For development we log in the 'dev' format.
app.configure('development', function() {
app.use( express.logger('dev') );
});
// Log the real IP address when behind a (reverse) proxy.
express.logger.token('remote-addr', function(req, res) {
var ip = req.header('x-forwarded-for');
if( ip ) {
// There can be multiple IP addresses if there are more than 1 proxies. Get the first one.
return ip.split(',')[0];
} else {
// When there are no proxies; get the normal IP address.
return req.connection.remoteAddress;
}
});
app.configure(function() {
app.set( 'views', __dirname + '/views' );
app.set( 'view engine', 'jade' );
app.use( express.bodyParser() );
app.use( express.methodOverride() );
app.use('/', express.static(__dirname + '/public', { maxAge: 24*60*60*1000 }) );
app.use( app.router );
});
// Different kind of error exposure on development and production.
// Don't minify the HTML in development.
app.configure('development', function() {
app.use( express.errorHandler({ dumpExceptions: true, showStack: true }) );
app.set('view options', { pretty: true, layout: false });
});
app.configure('production', function() {
app.use( express.errorHandler() );
app.set('view options', { pretty: false, layout: false });
});
/**
* Routes
*/
app.get('/', routes.index);
app.get('/apps', apps.getAppsCount, routes.apps);
app.get('/welkom', routes.welkom);
app.get('/contact', vestigingen.getVestigingen, routes.contact);
app.get('/privacy', routes.privacy);
app.get('/vereniging', routes.vereniging);
app.get('/wegenwacht', apps.getApps, routes.wegenwacht);
app.get('/verkeer', verkeer.getFeed, verkeer.getTrafficJamFeed, apps.getApps, routes.verkeer);
app.get('/apps/:platform', apps.getApps, routes.apps_platform);
app.get('*', function(req, res) {
res.send('Pagina niet gevonden...', 404);
});
/**
* Download the verkeer feed from www.anwb.nl.
*/
verkeer.downloadVerkeerFeed( function( err, result ) {
verkeerFeedLoop(err, result);
});
// Download traffic jam information.
// FIXME: this seems to cause a memory leak of some sort.
// Disabling it for now to test this.
//verkeer.downloadTrafficjamFeed( trafficjamFeedLoop );
function trafficjamFeedLoop( err, result ) {
if( err ) {
console.warn( err );
} else if ( result ) {
util.log( result );
}
setTimeout(function() {
verkeer.downloadTrafficjamFeed( trafficjamFeedLoop );
}, verkeerFeedTimeout);
}
function verkeerFeedLoop( err, result ) {
if( err ) {
console.warn( err );
} else if ( result ) {
util.log( result );
}
setTimeout(function() {
verkeer.downloadVerkeerFeed( verkeerFeedLoop );
}, verkeerFeedTimeout);
}
/**
* Startup
*/
var port = process.env.PORT || 3000
, ip = process.env.IP || '0.0.0.0'
app.listen( port, ip, function() {
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});