ConnInfo-Helfer
Der ConnInfo-Helfer unterstützt dich beim Abrufen von Verbindungsinformationen. Beispielsweise kannst du damit einfach die Remote-Adresse des Clients ermitteln.
Import
Der Adapter für jede Laufzeitumgebung ist ein separates Paket (@hono/cloudflare-workers, @hono/deno, @hono/bun, ...). Installiere das Paket für deine Laufzeitumgebung.
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/aws-lambda'ts
import { Hono } from 'hono'
import { getConnInfo } from '@hono/netlify'ts
import { Hono } from 'hono'
import { getConnInfo } from '@hono/lambda-edge'ts
import { Hono } from 'hono'
import { getConnInfo } from '@hono/node-server/conninfo'Verwendung
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}`)
})Typdefinitionen
Die Typdefinitionen der Werte, die du mit getConnInfo() abrufen kannst, lauten wie folgt:
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
}