-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.mjs
More file actions
32 lines (30 loc) · 1.84 KB
/
Copy pathserve.mjs
File metadata and controls
32 lines (30 loc) · 1.84 KB
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
// Zero-dependency static file server with HTTP Range support. Serves the demo
// locally for the static acts (②③④⑥); the public copy is on GitHub Pages.
// node serve.mjs # http://localhost:8088
// PORT=9000 node serve.mjs
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const DIR = path.dirname(fileURLToPath(import.meta.url));
const PORT = Number(process.env.PORT || 8088);
const TYPES = { '.html':'text/html', '.js':'text/javascript', '.mjs':'text/javascript', '.json':'application/json',
'.jsonld':'application/json', '.ndjson':'application/x-ndjson', '.dat':'application/octet-stream',
'.hex':'text/plain', '.txt':'text/plain', '.map':'application/json', '.css':'text/css', '.wasm':'application/wasm' };
http.createServer((req, res) => {
const name = decodeURIComponent(req.url.split('?')[0]).replace(/^\/+/, '') || 'index.html';
const file = path.join(DIR, path.normalize(name));
if (!file.startsWith(DIR) || !fs.existsSync(file) || fs.statSync(file).isDirectory()) { res.writeHead(404); return res.end('not found'); }
const size = fs.statSync(file).size;
const base = { 'Content-Type': TYPES[path.extname(file)] || 'application/octet-stream', 'Access-Control-Allow-Origin': '*', 'Accept-Ranges': 'bytes' };
const range = req.headers.range;
if (range) {
const m = /bytes=(\d*)-(\d*)/.exec(range);
const start = m[1] ? Number(m[1]) : 0, end = m[2] ? Number(m[2]) : size - 1;
res.writeHead(206, { ...base, 'Content-Range': `bytes ${start}-${end}/${size}`, 'Content-Length': end - start + 1 });
fs.createReadStream(file, { start, end }).pipe(res);
} else {
res.writeHead(200, { ...base, 'Content-Length': size });
fs.createReadStream(file).pipe(res);
}
}).listen(PORT, () => console.log(`serving ${DIR} on http://localhost:${PORT}`));