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#

OptionTypeDefaultDescription
portnumber3000Port to listen on. 0 = random available port
hostnamestring"localhost"Hostname to bind to
routesRecord<string, unknown>Bun routes object — maps paths to HTML files or handlers
fetch(req: Request) => Response404 handlerCustom fetch handler — used when no routes provided
hmrbooleantrueEnable Hot Module Replacement
consolebooleantrueEcho browser console to terminal
onReady(info) => voidCalled when server starts
...restBunServeOptionsAny 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 serversbun --watch server.ts is 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 server
Info
devServer() without routes or fetch returns 404 for all requests by default. This is useful for testing that the server starts correctly.