Skip to main content

ConnInfo Helper

还没翻译译自英文(hono.dev)本页 9 段都还没译

ConnInfo Helper

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

Import

The adapter for each runtime is a separate package (@hono/cloudflare-workers, @hono/deno, @hono/bun, ...). Install the one for your runtime.

Cloudflare WorkersDenoBunVercelAWS LambdaNetlifyLambda@EdgeNode.js

import { Hono } from 'hono'
import { getConnInfo } from '@hono/cloudflare-workers'

Usage

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

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

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
}