40 lines
1001 B
TypeScript
40 lines
1001 B
TypeScript
import { serve } from "bun";
|
|
|
|
serve({
|
|
port: 3001,
|
|
// hostname: "fpga.swordlost.com",
|
|
routes: {
|
|
// Static routes
|
|
"/api/status": new Response("OK"),
|
|
|
|
// Dynamic routes
|
|
"/users/:id": req => {
|
|
return new Response(`Hello User ${req.params.id}!`);
|
|
},
|
|
|
|
// Per-HTTP method handlers
|
|
"/api/posts": {
|
|
GET: () => new Response("List posts"),
|
|
POST: async req => {
|
|
const body = await req.json();
|
|
|
|
if (body !== null) {
|
|
return Response.json({ created: true, ...body });
|
|
} else {
|
|
return Response.json({})
|
|
}
|
|
},
|
|
},
|
|
|
|
// Wildcard route for all routes that start with "/api/" and aren't otherwise matched
|
|
"/api/*": Response.json({ message: "Not found" }, { status: 404 }),
|
|
|
|
// Serve a file by buffering it in memory
|
|
// "/favicon.ico": new Response(await Bun.file("../public/favicon.icon").bytes(), {
|
|
// headers: {
|
|
// "Content-Type": "image/x-icon",
|
|
// },
|
|
// }),
|
|
}
|
|
});
|