The Proxy Helper provides functionality to proxy requests to backend servers with automatic header handling.
Import
import { proxy } from 'hono/proxy'
Functions
proxy()
Proxy a request to a backend server.
function proxy(url: string | URL, options?: ProxyOptions): Response | Promise<Response>
The target URL to proxy to
Optional proxy configuration
return
Response | Promise<Response>
The proxied response
Example
import { proxy } from 'hono/proxy'
app.get('/proxy', (c) => {
return proxy('https://api.example.com')
})
Options
type ProxyOptions = {
fetch?: typeof fetch
}
Custom fetch implementation
Advanced Usage
import { proxy } from 'hono/proxy'
// Proxy with path forwarding
app.get('/api/*', (c) => {
const path = c.req.path.replace(/^\/api/, '')
return proxy(`https://backend.com${path}`)
})
// Proxy with custom fetch
app.get('/secure/*', (c) => {
return proxy('https://backend.com', {
fetch: customFetch
})
})
Security Features
- Automatically removes hop-by-hop headers
- Prevents Connection header injection
- Handles encoding properly
- Preserves request body and method