Skip to content

요청 ID 미들웨어

요청 ID 미들웨어는 각 요청마다 고유한 ID를 생성한다. 이 ID를 핸들러에서 활용할 수 있다.

Import

ts
import { Hono } from 'hono'
import { requestId } from 'hono/request-id'

사용 방법

Request ID 미들웨어가 적용된 핸들러와 미들웨어에서 requestId 변수를 통해 Request ID에 접근할 수 있다.

ts
const app = new Hono()

app.use('*', requestId())

app.get('/', (c) => {
  return c.text(`요청 ID: ${c.get('requestId')}`)
})

타입을 명시적으로 지정하려면 RequestIdVariables를 임포트하고 new Hono()의 제네릭으로 전달한다.

ts
import type { RequestIdVariables } from 'hono/request-id'

const app = new Hono<{
  Variables: RequestIdVariables
}>()

옵션

선택사항 limitLength: number

요청 ID의 최대 길이를 지정한다. 기본값은 255이다.

optional headerName: string

요청 ID에 사용할 헤더 이름이다. 기본값은 X-Request-Id이다.

optional generator: (c: Context) => string

요청 ID를 생성하는 함수이다. 기본적으로 crypto.randomUUID()를 사용한다.

Released under the MIT License.