-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstatic.js
37 lines (30 loc) · 1.09 KB
/
static.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
const { program } = require('commander');
const path = require('path');
const KOA = require('koa');
const http = require('http');
const koaCompress = require('koa-compress');
const helmet = require('koa-helmet');
program
.option('-p, --port <number>', 'Server port(default 1086)', '1086')
.option('--host <char>', 'Server host(default 0.0.0.0)', '0.0.0.0')
.option('-a, --age <char>', 'max age(default 2592000)', '2592000')
.option('-d, --dir [dirs...]', 'target directory(default ./)', './');
program.parse();
const options = program.opts();
const staticServe = (path) => {
return require('koa-static')(path, {
setHeaders: function (response) {
response.setHeader('Cache-Control', `public, max-age=${options.age}`);
},
});
};
const app = new KOA();
app.use(koaCompress({ threshold: 2048 }));
for (const dir of options.dir) {
app.use(staticServe(path.resolve(dir)));
}
app.use(helmet());
const staticServer = http.createServer(app.callback());
staticServer.listen(options.port, options.host, () => {
console.log(`Static service is running at ${options.host}:${options.port}`);
});