ConnInfo Helper
ConnInfo Helperは接続情報を取得するのに役立ちます。例えば、クライアントのリモートアドレスを簡単に取得することができます。
インポート
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/cloudflare-workers'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/deno'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/bun'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/vercel'
ts
import { Hono } from 'hono'
import { getConnInfo } from 'hono/lambda-edge'
ts
import { Hono } from 'hono'
import { getConnInfo } from '@hono/node-server/conninfo'
使用法
ts
const app = new Hono()
app.get('/', (c) => {
const info = getConnInfo(c) // info is `ConnInfo`
return c.text(`Your remote address is ${info.remote.address}`)
})
型の定義
getConnInfo()
から取得できる値の型の定義を以下に示します。
ts
type AddressType = 'IPv6' | 'IPv4' | undefined
type NetAddrInfo = {
/**
* Transport protocol type
*/
transport?: 'tcp' | 'udp'
/**
* Transport port number
*/
port?: number
address?: string
addressType?: AddressType
} & (
| {
/**
* Host name such as IP Addr
*/
address: string
/**
* Host name type
*/
addressType: AddressType
}
| {}
)
/**
* HTTP Connection information
*/
interface ConnInfo {
/**
* Remote information
*/
remote: NetAddrInfo
}