forked from confcodeofconduct/confcodeofconduct.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (46 loc) · 1.36 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
'use strict';
var st = require('st');
var http = require('http');
var base = 'confcodeofconduct.com';
var parse = require('url').parse;
var mount = st({
path: __dirname,
// url: '/',
index: 'index.html', // server index.html for directories
// passthrough: true // pass through if not found, so we can send 404
});
function subdomain(req, res, next) {
var proto = req.socket.encrypted ? 'https' : 'http';
var hostparsed = parse(proto + '://' + req.headers.host + req.url);
var host = hostparsed.hostname;
var www = /^www\./;
if (www.test(host)) {
// hard coded http for now
var url = proto + '://' + hostparsed.host.replace(www, '') + req.url;
res.writeHead(302, { location: url });
return res.end();
}
if (host.indexOf(base) === 0 || host.indexOf('localhost:') === 0) {
next(); // as you were
} else {
var matches = (host.match(/^(.*?)\./) || []);
// subdomain
if (matches.length === 2) {
req.url = 'index-' + matches[1] + '.html';
next();
} else {
next();
}
}
}
var server = http.createServer(function(req, res) {
// console.log(req.headers);
subdomain(req, res, function () {
mount(req, res, function () {
res.writeHead(404);
res.end('404');
});
});
}).listen(process.env.PORT || 0, function () {
console.log('Running on http://localhost:' + server.address().port);
});