-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
executable file
·56 lines (44 loc) · 1.19 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
52
53
54
55
56
#!/usr/bin/env node
'use strict';
const argv = require('minimist')(process.argv.slice(2), {
default: {port: 8000}
});
if (argv.help) {
console.log(
`./server.js
Options:
--port Port number (default: 8000)
--http2 Use HTTP2 server (HTTPS)
--max-age Set a max-age on all resources in ms (default: 0)
--no-store Set no-store on all resources
`
)
process.exit();
}
const fs = require('fs');
const connect = require('connect');
const morgan = require('morgan');
const serveIndex = require('serve-index');
const serveStatic = require('serve-static');
const app = connect();
app.use(morgan('dev'));
const staticOpts = {
maxAge: argv['max-age'] || 0
};
if (argv['no-store']) {
staticOpts.setHeaders = res => res.setHeader('Cache-Control', 'no-store');
}
app.use('/', serveStatic('public', staticOpts));
app.use('/', serveIndex('public', {icons: true}));
const server = (() => {
if (argv.http2) {
return require('http2').createServer({
key: fs.readFileSync('./key.pem'),
cert: fs.readFileSync('./certificate.pem')
}, app);
}
return require('http').createServer(app);
})();
server.listen(argv.port, () => {
console.log(`Listening on port ${argv.port}!`);
});