Under The Hood
frontend bun Bun 1.x (`Bun.serve`)

Bun.serve(): uWebSockets C++ Engine & Zero-Copy HTTP Routing

Last updated
Prerequisites:
Node.js `http.createServer`
Fetch API `Request`/`Response`
  • bun
  • http-server
  • bun-serve
  • uwebsockets
  • networking
  • performance

Read at your depth

The practical view

`Bun.serve()` is Bun's built-in HTTP and WebSocket server: `Bun.serve({ port: 3000, fetch(req) { return new Response('Hello'); } })`. It uses standard Web `Request` and `Response` objects. It includes built-in static file routing, TLS/HTTPS support, Server-Sent Events (SSE), and native WebSocket handling with zero npm dependencies.

The same idea in other frameworks

node equivalent
`http.createServer((req, res) => res.end('Hello')).listen(3000);`

Legacy vs modern

Node.js http.createServer vs Bun.serve Native Server

Node requires manual stream and socket wrapping; `Bun.serve` uses Web Standards with high-performance C++ networking.

before → after
Node.js http.createServer
http.createServer((req, res) => { res.writeHead(200); res.end('OK'); }).listen(3000); // ❌ Node stream API
Bun.serve() Web Standard API
Bun.serve({ port: 3000, fetch(req) { return Response.json({ status: 'healthy' }); } }); // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of Bun's native WebSocket server integration.

The mechanical answer

`Bun.serve()` includes built-in WebSocket support using the `websocket` configuration object: `Bun.serve({ fetch(req, server) { if (server.upgrade(req)) return; return new Response('HTTP'); }, websocket: { open(ws) {}, message(ws, msg) {}, close(ws) {} } })`. The upgrade from HTTP to WebSocket happens in native C++ with zero npm dependencies.

Trap

You do not need to install `npm install ws` in Bun; use `server.upgrade(req)` directly.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.