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

Import

import { 
  serveStatic, 
  toSSG, 
  denoFileSystemModule, 
  upgradeWebSocket, 
  getConnInfo 
} from 'hono/deno'

Functions

serveStatic()

Middleware for serving static files from the local file system in Deno.
function serveStatic<E extends Env = Env>(
  options: ServeStaticOptions<E>
): MiddlewareHandler

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

import { Hono } from 'hono'
import { serveStatic } from 'hono/deno'

const app = new Hono()

// Serve files from ./public directory
app.use('/static/*', serveStatic({ root: './public' }))

// Serve with path rewriting
app.use('/assets/*', serveStatic({
  root: './public',
  rewriteRequestPath: (path) => path.replace(/^\/assets/, '')
}))

Deno.serve(app.fetch)

upgradeWebSocket()

Helper function to upgrade HTTP connections to WebSocket connections in Deno.
function upgradeWebSocket<T>(
  createEvents: (c: Context) => WSEvents,
  options?: Deno.UpgradeWebSocketOptions
): MiddlewareHandler

Parameters

  • createEvents - Function that receives context and returns WebSocket event handlers:
    • onOpen - Called when the connection is established
    • onMessage - Called when a message is received
    • onClose - Called when the connection closes
    • onError - Called on error
  • options - Deno-specific WebSocket upgrade options (optional)

Example

import { Hono } from 'hono'
import { upgradeWebSocket } from 'hono/deno'

const app = new Hono()

app.get(
  '/ws',
  upgradeWebSocket((c) => {
    return {
      onOpen: (evt, ws) => {
        console.log('WebSocket connection opened')
        ws.send('Welcome!')
      },
      onMessage: (event, ws) => {
        console.log(`Message: ${event.data}`)
        ws.send(`Echo: ${event.data}`)
      },
      onClose: (evt, ws) => {
        console.log('Connection closed')
      },
      onError: (evt, ws) => {
        console.error('WebSocket error:', evt)
      },
    }
  })
)

Deno.serve(app.fetch)

toSSG()

toSSG is an experimental feature. The API might be changed.
Generates static HTML files from your Hono application (Static Site Generation).
function toSSG(
  app: Hono,
  options?: ToSSGOptions
): Promise<ToSSGResult>

Parameters

  • app - The Hono application instance
  • options - SSG options:
    • dir - string (optional) - Output directory (default: './static')
    • beforeRequestHook - (req: Request) => Request | false (optional)
    • afterResponseHook - (res: Response) => Response | false (optional)
    • afterGenerateHook - (result: ToSSGResult) => void | Promise<void> (optional)

Example

import { Hono } from 'hono'
import { toSSG } from 'hono/deno'

const app = new Hono()

app.get('/', (c) => c.html('<h1>Home</h1>'))
app.get('/about', (c) => c.html('<h1>About</h1>'))
app.get('/posts/:id', (c) => {
  const { id } = c.req.param()
  return c.html(`<h1>Post ${id}</h1>`)
})

// Generate static files
await toSSG(app, {
  dir: './dist',
  afterGenerateHook: async (result) => {
    console.log(`Generated ${result.files.length} files`)
    console.log('Success:', result.success)
  }
})

denoFileSystemModule

denoFileSystemModule is an experimental feature. The API might be changed.
File system module implementation for Deno, used by toSSG.
const denoFileSystemModule: FileSystemModule

Interface

interface FileSystemModule {
  writeFile(path: string, data: string | Uint8Array): Promise<void>
  mkdir(path: string, options?: { recursive?: boolean }): Promise<void>
}

getConnInfo()

Extracts connection information from the Deno request.
function getConnInfo(c: Context): ConnInfo

Returns

interface ConnInfo {
  remote: {
    address?: string     // Client IP address
    port?: number       // Client port
    transport?: string  // Transport protocol (e.g., 'tcp', 'udp')
  }
}

Example

import { Hono } from 'hono'
import { getConnInfo } from 'hono/deno'

const app = new Hono()

app.get('/', (c) => {
  const info = getConnInfo(c)
  return c.json({
    ip: info.remote.address,
    port: info.remote.port,
    transport: info.remote.transport
  })
})

Deno.serve(app.fetch)

Platform-Specific Notes

  • Deno uses Deno.open() and Deno.lstatSync() for file operations
  • Connection information is extracted from c.env.remoteAddr
  • WebSocket support uses Deno.upgradeWebSocket()
  • SSG functionality uses Deno’s file system APIs
  • Deno requires explicit permissions:
    • --allow-read for serving static files
    • --allow-write for SSG
    • --allow-net for serving HTTP

Running Your Application

# Development
deno run --allow-net --allow-read main.ts

# SSG generation
deno run --allow-net --allow-read --allow-write generate.ts

# With watch mode
deno run --watch --allow-net --allow-read main.ts

See Also

Build docs developers (and LLMs) love