Bun.serve() Basics in Bun
From the Bun cheat sheet ยท HTTP Server ยท verified Jul 2026
Bun.serve() Basics
Create an HTTP server with routes and request handling
typescript
Bun.serve({
port: 3000,
routes: {
"/": new Response("Hello!"),
"/api/users/:id": (req) => {
return Response.json({ id: req.params.id });
},
},
fetch(req) {
return new Response("Not Found", { status: 404 });
},
});๐ก Bun.serve uses Web Standard Request/Response โ no framework-specific APIs to learn
โก Routes with method handlers (GET/POST) require Bun v1.2.3+ โ use fetch() fallback for older versions
๐ Bun.file() in routes lazily loads files into memory โ efficient for static assets
๐ข server.reload() hot-swaps the config without dropping existing connections
serverhttproutesapi