Skip to content

ConnInfo 助手

¥ConnInfo Helper

ConnInfo Helper 可帮助你获取连接信息。例如,你可以轻松获取客户端的远程地址。

¥The ConnInfo Helper helps you to get the connection information. For example, you can get the client's remote address easily.

导入

¥Import

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'

用法

¥Usage

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}`)
})

类型定义

¥Type Definitions

你可以从 getConnInfo() 获取的值的类型定义如下:

¥The type definitions of the values that you can get from getConnInfo() are the following:

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
}

Hono v4.7 中文网 - 粤ICP备13048890号