Dev Server#
Bun development server with HMR (Hot Module Replacement) enabled. Thin wrapper around Bun.serve() with development: true — automatic re-bundling, source maps, and hot reload when files change.
Import#
typescript
import { devServer } from "@buntok/core/dev";Usage#
Minimal — default returns 404#
typescript
import { devServer } from "@buntok/core/dev";
devServer({
port: 3000,
onReady: (info) => console.log(`Dev server: http://localhost:${info.port}`),
});With routes — serve HTML files#
typescript
import { devServer } from "@buntok/core/dev";
import homepage from "./index.html";
devServer({
port: 3000,
routes: { "/": homepage },
onReady: (info) => console.log(`Dev server: http://localhost:${info.port}`),
});With custom fetch handler#
typescript
import { devServer } from "@buntok/core/dev";
devServer({
port: 3000,
fetch: (req) => new Response(`Hello from ${req.url}`),
});Options#
| Option | Type | Default | Description |
|---|---|---|---|
| port | number | 3000 | Port to listen on. 0 = random available port |
| hostname | string | "localhost" | Hostname to bind to |
| routes | Record<string, unknown> | — | Bun routes object — maps paths to HTML files or handlers |
| fetch | (req: Request) => Response | 404 handler | Custom fetch handler — used when no routes provided |
| hmr | boolean | true | Enable Hot Module Replacement |
| console | boolean | true | Echo browser console to terminal |
| onReady | (info) => void | — | Called when server starts |
| ...rest | BunServeOptions | — | Any additional Bun.serve() options |
When to Use#
- Web/frontend development — HMR for HTML, CSS, JS changes
- Full-stack apps — serve templates + API routes simultaneously
- Framework development — test BunTok itself with hot reload
When NOT to Use#
- Pure API servers —
bun --watch server.tsis simpler and sufficient - Vercel deployment — Vercel handles serving; use
app.fetch()instead - Production — always use
bun run build+bun run start
Return Value#
Returns the Bun.serve() instance:
typescript
const server = devServer({ port: 0 });
console.log(server.port); // actual port (useful with port: 0)
server.stop(); // stop the serverInfo
devServer() without routes or fetch returns 404 for all requests by default. This is useful for testing that the server starts correctly.