type

Serve.Options

type Options<WebSocketData, R extends string = string> = Bun.__internal.XOR<HostnamePortServeOptions<WebSocketData>, UnixServeOptions<WebSocketData>> & Bun.__internal.XOR<FetchOrRoutes<WebSocketData, R>, FetchOrRoutesWithWebSocket<WebSocketData, R>>

Options for serve, with support for routes and a safer requirement for fetch

export default {
  fetch: req => Response.json(req.url),

  websocket: {
    message(ws) {
      ws.data.name; // string
    },
  },
} satisfies Bun.Serve.Options<{ name: string }>;

Referenced types

type XOR<A, B> = Without<A, B> | Without<B, A>

interface HostnamePortServeOptions<WebSocketData>

  • development?: Development

    Whether to render contextual errors with Bun's error page

  • error?: (this: Server<WebSocketData>, error: ErrorLike) => void | Promise<void> | Response | Promise<Response>

    Called when an error is thrown during request handling

    error: (error) => {
      return new Response("Internal Server Error", { status: 500 });
    }
  • hostname?: string & {} | '0.0.0.0' | '127.0.0.1' | 'localhost'

    The hostname the server listens on

    "127.0.0.1" // Only listen locally
  • http1?: boolean

    Listen for HTTP/1.1 over TCP. Set to false together with http3: true to serve HTTP/3 only.

  • http3?: boolean

    Also listen for HTTP/3 (QUIC) on the same port. Requires tls.

  • id?: null | string

    Uniquely identify a server instance with an ID


    When bun is started with the --hot flag:

    Bun uses this string to hot reload the server without interrupting pending requests or websockets. If not provided, a value is generated. To disable hot reloading, set this value to null.

    When bun is not started with the --hot flag:

    This string has no effect.

  • idleTimeout?: number

    Sets the number of seconds to wait before timing out a connection due to inactivity.

  • ipv6Only?: boolean

    Whether the IPV6_V6ONLY flag should be set.

  • maxRequestBodySize?: number

    The maximum size of a request body, in bytes

  • port?: string | number

    The port the server listens on

  • reusePort?: boolean

    Whether the SO_REUSEPORT flag should be set.

    This allows multiple processes to bind to the same port, which is useful for load balancing.

  • tls?: TLSOptions | TLSOptions[]

    Set options for using TLS with this server

    const server = Bun.serve({
      fetch: request => new Response("Welcome to Bun!"),
      tls: {
        cert: Bun.file("cert.pem"),
        key: Bun.file("key.pem"),
        ca: [Bun.file("ca1.pem"), Bun.file("ca2.pem")],
      },
    });

interface UnixServeOptions<WebSocketData>

  • development?: Development

    Whether to render contextual errors with Bun's error page

  • error?: (this: Server<WebSocketData>, error: ErrorLike) => void | Promise<void> | Response | Promise<Response>

    Called when an error is thrown during request handling

    error: (error) => {
      return new Response("Internal Server Error", { status: 500 });
    }
  • id?: null | string

    Uniquely identify a server instance with an ID


    When bun is started with the --hot flag:

    Bun uses this string to hot reload the server without interrupting pending requests or websockets. If not provided, a value is generated. To disable hot reloading, set this value to null.

    When bun is not started with the --hot flag:

    This string has no effect.

  • maxRequestBodySize?: number

    The maximum size of a request body, in bytes

  • tls?: TLSOptions | TLSOptions[]

    Set options for using TLS with this server

    const server = Bun.serve({
      fetch: request => new Response("Welcome to Bun!"),
      tls: {
        cert: Bun.file("cert.pem"),
        key: Bun.file("key.pem"),
        ca: [Bun.file("ca1.pem"), Bun.file("ca2.pem")],
      },
    });
  • unix?: string

    If set, the HTTP server listens on a unix socket instead of a port. (Cannot be used with hostname+port)

type FetchOrRoutes<WebSocketData, R extends string> =
| { routes: Routes<WebSocketData, R>; fetch(this: Server<WebSocketData>, req: Request, server: Server<WebSocketData>): MaybePromise<Response> }
| { routes: Routes<WebSocketData, R>; fetch(this: Server<WebSocketData>, req: Request, server: Server<WebSocketData>): MaybePromise<Response> }
type FetchOrRoutesWithWebSocket<WebSocketData, R extends string> = { websocket: WebSocketHandler<WebSocketData> } & { routes: RoutesWithUpgrade<WebSocketData, R>; fetch(this: Server<WebSocketData>, req: Request, server: Server<WebSocketData>): MaybePromise<undefined | void | Response> } | { routes: RoutesWithUpgrade<WebSocketData, R>; fetch(this: Server<WebSocketData>, req: Request, server: Server<WebSocketData>): MaybePromise<undefined | void | Response> }