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

More Bun tasks

Back to the full Bun cheat sheet