Skip to main content
The Bun adapter provides utilities for running Hono applications on Bun, including static file serving, SSG support, WebSocket handling, and server utilities.

Import

Functions

serveStatic()

Middleware for serving static files from the local file system in Bun.

Parameters

  • options.root - string (optional) - Root directory path (default: '.')
  • options.path - string (optional) - Base path for static files
  • options.rewriteRequestPath - (path: string) => string (optional) - Function to rewrite request paths
  • Additional options from base ServeStaticOptions

Example

upgradeWebSocket()

Helper function to upgrade HTTP connections to WebSocket connections in Bun.

Parameters

  • createEvents - Function that receives context and returns WebSocket event handlers

Example

websocket

Bun WebSocket handler that must be exported alongside your app for WebSocket functionality.

Example

createBunWebSocket()

createBunWebSocket is deprecated. Import upgradeWebSocket and websocket directly from hono/bun instead.
Legacy function to create a Bun WebSocket handler.

toSSG()

toSSG is an experimental feature. The API might be changed.
Generates static HTML files from your Hono application (Static Site Generation).

Example

bunFileSystemModule

bunFileSystemModule is an experimental feature. The API might be changed.
File system module implementation for Bun, used by toSSG.

getConnInfo()

Extracts connection information from the Bun request.

Returns

Example

getBunServer()

Gets the Bun server instance from the context.

Returns

The Bun server object, or undefined if not available.

Example

Types

BunWebSocketData

Internal data structure for WebSocket connections.

BunWebSocketHandler

Bun-specific WebSocket handler interface.

Platform-Specific Notes

  • Bun uses Bun.file() for optimized file serving
  • Connection info is retrieved via server.requestIP()
  • WebSocket support requires exporting the websocket handler alongside fetch
  • The server instance must be passed in the second argument of app.fetch() for getConnInfo and WebSocket to work
  • Bun provides excellent performance for static file serving

Running Your Application

Basic Server

With WebSocket

See Also