bun.server.ts (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
const server = Bun.serve({
port: Number(process.env.PORT || 3000),
async fetch(req) {
const url = new URL(req.url);
let pathname = url.pathname;
if (pathname === "/") pathname = "/index.html";
// Prevent path traversal
pathname = pathname.replace(/\.\.+/g, "");
const filePath = `.${pathname}`;
const file = Bun.file(filePath);
if (await file.exists()) {
return new Response(file);
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Server running at http://localhost:${server.port}`);
|