18 lines
1 KiB
TypeScript
18 lines
1 KiB
TypeScript
#!/usr/bin/env bun
|
|
// Static server for the demo app's web page. bun examples/demo-app/serve.ts [--port 8741]
|
|
const i = process.argv.indexOf('--port');
|
|
const port = Number(i > 0 ? process.argv[i + 1] : process.env.PORT ?? 8741);
|
|
const root = new URL('./web/', import.meta.url).pathname;
|
|
const TYPES: Record<string, string> = { html: 'text/html; charset=utf-8', js: 'text/javascript; charset=utf-8', css: 'text/css' };
|
|
const server = Bun.serve({
|
|
port, hostname: '127.0.0.1',
|
|
async fetch(req) {
|
|
let p = decodeURIComponent(new URL(req.url).pathname);
|
|
if (p.endsWith('/')) p += 'index.html';
|
|
if (p.includes('..')) return new Response('no', { status: 400 });
|
|
const f = Bun.file(root + p.slice(1));
|
|
if (!(await f.exists())) return new Response('not found', { status: 404 });
|
|
return new Response(f, { headers: { 'content-type': TYPES[p.split('.').pop() ?? ''] ?? 'application/octet-stream', 'cache-control': 'no-store' } });
|
|
},
|
|
});
|
|
console.log(`Tidy Lists demo on http://127.0.0.1:${server.port}/`);
|