> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/honojs/hono/llms.txt
> Use this file to discover all available pages before exploring further.

# Deno

> API reference for the Deno adapter

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

## Import

```ts theme={null}
import { 
  serveStatic, 
  toSSG, 
  denoFileSystemModule, 
  upgradeWebSocket, 
  getConnInfo 
} from 'hono/deno'
```

## Functions

### serveStatic()

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

```ts theme={null}
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

```ts theme={null}
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.

```ts theme={null}
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

```ts theme={null}
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()

<Note>
  `toSSG` is an experimental feature. The API might be changed.
</Note>

Generates static HTML files from your Hono application (Static Site Generation).

```ts theme={null}
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

```ts theme={null}
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

<Note>
  `denoFileSystemModule` is an experimental feature. The API might be changed.
</Note>

File system module implementation for Deno, used by `toSSG`.

```ts theme={null}
const denoFileSystemModule: FileSystemModule
```

#### Interface

```ts theme={null}
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.

```ts theme={null}
function getConnInfo(c: Context): ConnInfo
```

#### Returns

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

#### Example

```ts theme={null}
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

```bash theme={null}
# 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

* [Deno Documentation](https://deno.land/manual)
* [Deno Deploy](https://deno.com/deploy)
* [Deno.serve API](https://deno.land/api?s=Deno.serve)
