This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 433
/
Copy pathhttpd.js
67 lines (59 loc) · 1.83 KB
/
httpd.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
/* eslint-env node, es6 */
'use strict';
const
nodeStaticAlias = require('node-static-alias'),
log4js = require('log4js'),
http = require('http'),
DOC_ROOT = __dirname,
PORT = 8080,
MODULE_PACKAGES = [
'jasmine-core',
'test-page-loader',
'anim-event',
'plain-draggable'
],
logger = (() => {
log4js.configure({ // Super simple format
appenders: {out: {type: 'stdout', layout: {type: 'pattern', pattern: '%[[%r]%] %m'}}},
categories: {default: {appenders: ['out'], level: 'info'}}
});
return log4js.getLogger('node-static-alias');
})(),
staticAlias = new nodeStaticAlias.Server(DOC_ROOT, {
cache: false,
headers: {'Cache-Control': 'no-cache, must-revalidate'},
alias:
MODULE_PACKAGES.map(packageName =>
({ // node_modules
match: new RegExp(`^/${packageName}/.+`),
serve: `${require.resolve(packageName).replace(
// Include `packageName` for nested `node_modules`
new RegExp(`^(.*[/\\\\]node_modules)[/\\\\]${packageName}[/\\\\].*$`), '$1')}<% reqPath %>`,
allowOutside: true
})
).concat([
{
match: /^\/src/,
serve: '..<% reqPath %>',
allowOutside: true
}
]),
logger
});
http.createServer((request, response) => {
request.addListener('end', () => {
staticAlias.serve(request, response, error => {
if (error) {
response.writeHead(error.status, error.headers);
logger.error('(%s) %s', request.url, response.statusCode);
if (error.status === 404) {
response.end('Not Found');
}
} else {
logger.info('(%s) %s', request.url, response.statusCode);
}
});
}).resume();
}).listen(PORT);
console.log(`START: http://localhost:${PORT}/\nROOT: ${DOC_ROOT}`);
console.log('(^C to stop)');