-
Notifications
You must be signed in to change notification settings - Fork 586
/
index.js
31 lines (25 loc) · 1.08 KB
/
index.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
/* eslint-disable no-console */
const http = require('http');
const finalhandler = require('finalhandler');
const serveStatic = require('serve-static');
// utilize the same pattern for exiting port and cache settings
const serverPortNumber = process.env.SERVER_PORT || 8888;
const args = process.argv.slice(2);
const disableCache = Array.isArray(args) && args.includes('--no-cache');
// disable cache
const setCustomCacheControl = (res, path) => {
console.log(`[HTTP Server] serving resource: ${path}`);
if (disableCache) {
res.setHeader('Cache-Control', 'public, max-age=0');
}
};
// serve root folder supporting relative path for accessing resources and cache settings
const serve = serveStatic('./', {
setHeaders: setCustomCacheControl,
});
const httpServer = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res));
});
httpServer.listen(serverPortNumber);
console.log(`[HTTP Server] serving at: http://localhost:${serverPortNumber.toString().trim()}/Simulator/index.html`);
console.log(`[HTTP Server] disable serving resources with cache: ${disableCache}`);