The Connection Info Helper provides utilities for retrieving connection information such as remote address and IP.
Import
import { getConnInfo } from 'hono/conninfo'
Functions
getConnInfo()
Get connection information from the request.
function getConnInfo(c: Context): ConnInfo
Connection information object
Example
import { getConnInfo } from 'hono/conninfo'
app.get('/', (c) => {
const info = getConnInfo(c)
return c.json({
remote: info.remote,
})
})
Types
type ConnInfo = {
remote: NetAddrInfo
}
type NetAddrInfo = {
address?: string
addressType?: AddressType
port?: number
family?: 'IPv4' | 'IPv6'
transport?: 'tcp' | 'udp'
}
type AddressType = 'IPv4' | 'IPv6' | 'unknown'
Adapter-Specific Usage
import { getConnInfo } from 'hono/adapter'
// Cloudflare Workers
import { getConnInfo } from 'hono/cloudflare-workers'
const info = getConnInfo(c)
console.log(info.remote.address) // From cf-connecting-ip header
// Deno
import { getConnInfo } from 'hono/deno'
const info = getConnInfo(c)
console.log(info.remote.port) // Server port
// Bun
import { getConnInfo } from 'hono/bun'
const info = getConnInfo(c)
// Node.js
import { getConnInfo } from 'hono/node-server'
const info = getConnInfo(c)