Middleware
Docs Home | Previous: Interceptors | Next: Response Validation
Middleware functions wrap route handling and are useful for request-scoped context and cross-cutting behavior. They are the outermost layer in the request pipeline — they execute before guards, interceptors, and controller methods.
Middleware Signature
Each middleware receives the Request and a next() function. Call next() to continue to the next middleware or the guard/interceptor/method chain. Return a Response directly to short-circuit.
Where Middleware Sits in the Pipeline
Middleware wraps everything — including guards, interceptors, and the controller method. This makes middleware the right place for:
- Request ID setup (before guards need it)
- User context extraction
- Request timing (measures the full pipeline)
- Logging
Registration Levels
Execution Order
When multiple middleware are registered at different levels, they execute in this order:
Each level wraps the next. The first global middleware is the outermost wrapper; the last method middleware is the innermost before guards execute.
Short-Circuiting
A middleware can return a Response without calling next() to skip all downstream processing:
Example
See examples/08_logging/request-context.ts and examples/08_logging/server.ts.