19 lines
1.2 KiB
JavaScript
19 lines
1.2 KiB
JavaScript
import http from 'node:http';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { fileURLToPath } from 'node:url';
|
|
import path from 'node:path';
|
|
const base = fileURLToPath(new URL('./dist/', import.meta.url));
|
|
const mime = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.json': 'application/json; charset=utf-8', '.css': 'text/css; charset=utf-8', '.ttf': 'font/ttf' };
|
|
const port = Number(process.env.ORGCHART_PORT || 4178);
|
|
http.createServer(async (request, response) => {
|
|
try {
|
|
const url = new URL(request.url, 'http://localhost');
|
|
const file = path.resolve(base, '.' + decodeURIComponent(url.pathname === '/' ? '/index.html' : url.pathname));
|
|
const relative = path.relative(base, file);
|
|
if (relative.startsWith('..') || path.isAbsolute(relative)) { response.writeHead(403).end(); return; }
|
|
const data = await readFile(file);
|
|
response.setHeader('Content-Type', mime[path.extname(file)] || 'application/octet-stream');
|
|
response.setHeader('Cache-Control', 'no-store'); response.end(data);
|
|
} catch { response.writeHead(404).end(); }
|
|
}).listen(port, '127.0.0.1', () => console.log(`OrgChart example: http://127.0.0.1:${port}`));
|