Http
The main HTTP server class for creating and configuring HTTP servers in Buntal. Route methods return the instance, so calls can be chained, and different methods on the same path are all kept. File routes (appDir) dispatch only exported GET, POST, PUT, PATCH, DELETE, OPTIONS and HEAD (HEAD falls back to GET). Global middlewares run for programmatic routes, file routes and preflight requests.
Source
Type Definition
class Http {
constructor(config: Config)
start(cb?: (server: Bun.Server) => void): Bun.Server
use(handler: AtomicHandler): void
get<R extends string, P = ExtractRouteParams<R>>(route: R, ...handlers: AtomicHandler<P>[]): this
post<R extends string, P = ExtractRouteParams<R>>(route: R, ...handlers: AtomicHandler<P>[]): this
put<R extends string, P = ExtractRouteParams<R>>(route: R, ...handlers: AtomicHandler<P>[]): this
patch<R extends string, P = ExtractRouteParams<R>>(route: R, ...handlers: AtomicHandler<P>[]): this
delete<R extends string, P = ExtractRouteParams<R>>(route: R, ...handlers: AtomicHandler<P>[]): this
options<R extends string, P = ExtractRouteParams<R>>(route: R, ...handlers: AtomicHandler<P>[]): this
route<R extends string, P = ExtractRouteParams<R>>(method: Method, route: R, ...handlers: AtomicHandler<P>[]): this
onError(handler: (error: Error) => Response): void
onNotFound(handler: AtomicHandler): void
}Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
config | Config | ✅ | - | Configuration object for the HTTP server |
Methods
constructor
Creates a new HTTP server instance with the specified configuration.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
config | Config | ✅ | - | Server configuration including port, appDir, websocket handler, and injectHandler |
start
Starts the HTTP server and begins listening for requests.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
cb | (server: Bun.Server) => void | ❌ | - | Optional callback function called when server starts |
use
Adds middleware to the server that runs for all requests.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
handler | AtomicHandler | ✅ | - | Middleware function to execute |
get
Registers a GET route handler.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
route | string | ✅ | - | Route pattern (supports parameters like /users/:id) |
...handlers | AtomicHandler[] | ✅ | - | One or more handler functions |
post
Registers a POST route handler.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
route | string | ✅ | - | Route pattern (supports parameters like /users/:id) |
...handlers | AtomicHandler[] | ✅ | - | One or more handler functions |
put
Registers a PUT route handler.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
route | string | ✅ | - | Route pattern (supports parameters like /users/:id) |
...handlers | AtomicHandler[] | ✅ | - | One or more handler functions |
patch
Registers a PATCH route handler.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
route | string | ✅ | - | Route pattern (supports parameters like /users/:id) |
...handlers | AtomicHandler[] | ✅ | - | One or more handler functions |
delete
Registers a DELETE route handler.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
route | string | ✅ | - | Route pattern (supports parameters like /users/:id) |
...handlers | AtomicHandler[] | ✅ | - | One or more handler functions |
onError
Sets a global error handler for the server.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
handler | (error: Error) => Response | Promise<Response> | ✅ | - | Function to handle errors |
onNotFound
Sets a handler for 404 Not Found responses.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
handler | AtomicHandler | ✅ | - | Function to handle 404 requests |
Last modified: 2026-10-02