Utilitaire ConnInfo
L’utilitaire ConnInfo permet d’obtenir les informations de connexion. Vous pouvez, par exemple, récupérer facilement l’adresse distante du client.
Importation
L’adaptateur de chaque environnement d’exécution est fourni dans un paquet distinct (@hono/cloudflare-workers, @hono/deno, @hono/bun, ...). Installez celui qui correspond à votre environnement.
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'Utilisation
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}`)
})Définitions des types
Les types des valeurs que vous pouvez obtenir avec getConnInfo() sont définis comme suit :
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
}