-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
44 lines (37 loc) · 1 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
var union = require('union'),
ecstatic = require('ecstatic');
var port = process.env.PORT || 8080;
//
// ### redirect(res)
// World's simplest redirect function
//
function redirect(res) {
var url = 'http://2015.empirenode.org',
body = '<p>301. Redirecting to <a href="' + url + '">' + url + '</a></p>';
res.writeHead(301, { 'content-type': 'text/html', location: url });
res.end(body);
}
var server = union.createServer({
before: [
function (req, res) {
var host = req.headers.host || '',
parts = host.split('.');
console.log('%s - %s%s', req.method, host, req.url);
if (process.env.NODE_ENV === 'production'
&& (parts.length !== 3 || parts[0] === 'www')) {
return redirect(res);
}
res.emit('next');
},
ecstatic({
root: __dirname + '/public',
defaultExt: true
}),
function (req, res) {
return redirect(res);
}
]
});
server.listen(port, function () {
console.log('Listening on %s', port);
});