16 lines
1.1 KiB
JavaScript
16 lines
1.1 KiB
JavaScript
import http from 'node:http';
|
|
import { readFile, stat } from 'node:fs/promises';
|
|
import { resolve, extname, sep } from 'node:path';
|
|
const root=resolve(process.argv.includes('--dist')?'dist':'.');
|
|
const port=Number(process.env.PORT||4173);
|
|
const types={'.html':'text/html; charset=utf-8','.js':'text/javascript; charset=utf-8','.css':'text/css; charset=utf-8','.json':'application/json; charset=utf-8','.svg':'image/svg+xml','.png':'image/png'};
|
|
http.createServer(async(req,res)=>{
|
|
try {
|
|
const url=new URL(req.url,'http://localhost');
|
|
const path=resolve(root,'.'+decodeURIComponent(url.pathname==='/'?'/index.html':url.pathname));
|
|
if(path!==root&&!path.startsWith(root+sep)){res.writeHead(403);res.end('Forbidden');return;}
|
|
if(!(await stat(path)).isFile()){res.writeHead(404);res.end('Not found');return;}
|
|
const body=await readFile(path);res.writeHead(200,{'Content-Type':types[extname(path)]||'application/octet-stream','Cache-Control':'no-cache'});res.end(body);
|
|
}catch{res.writeHead(404);res.end('Not found');}
|
|
}).listen(port,'0.0.0.0',()=>console.log(`HEX TRAILS http://localhost:${port}`));
|