forked from Hylozoic/hylo-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (35 loc) · 1.07 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
var http = require('http'),
static = require('node-static'),
fileServer = new static.Server('./dist'),
_ = require('lodash'),
url = require('url'),
util = require('util');
module.exports = function(opts) {
http.createServer(function(req, res) {
var u = url.parse(req.url),
originalUrl = req.url,
changePathname = function(value) {
u.pathname = value;
req.url = url.format(u);
},
log = function(resolution) {
opts.log.writeln('%s %s %s', req.method, originalUrl, resolution);
};
// remove trailing slash
u.pathname = u.pathname.replace(/\/$/, '');
if (_.startsWith(u.pathname, '/assets')) {
changePathname(u.pathname.replace(/^\/assets/, ''));
}
// all local assets
fileServer.serve(req, res, function(err, result) {
if (err && err.status === 404) {
res.statusCode = 404;
res.end('404 Not Found');
log('→ Not Found');
} else {
log('→ OK');
}
});
}).listen(opts.port);
opts.log.writeln('listening on port ' + opts.port);
};