Skip to content
English

Middleware

Middleware = (req, next) => Promise<HttpResponse> | HttpResponse

Defined in: src/http/Route.ts:30

Per-request hook that runs around a handler. Receives the request and a next() thunk; either short-circuit by returning your own response, or call next() and pass its result through (optionally wrapped, decorated, or re-thrown).

Examples (all shipped in src/http/middleware/):

  • BearerTokenAuth({ tokens }) — checks Authorization: Bearer, short-circuits with 401 on mismatch.
  • IpAllowlist({ allow }) — checks remoteAddress (or a configured extractor) against a CIDR list, short-circuits with 403 if not allowed.

Throwing HttpError(status, msg) is the idiomatic short-circuit: the global error handler catches it and emits the right response.

HttpRequest

() => Promise<HttpResponse>

Promise<HttpResponse> | HttpResponse