Kind: Interface
Source: packages/common/interfaces/middleware/middleware-configuration.interface.ts
Part of: Common
MiddlewareConfiguration defines how a middleware component is bound to one or more application routes. It pairs a middleware class or instance with route targets expressed as controllers, path strings, or RouteInfo objects.
Properties
| Property | Type |
|---|---|
middleware | T |
forRoutes | `(Type |
Diagram
mermaidgraph LR MC[MiddlewareConfiguration] M[middleware: T] FR[forRoutes: Route Targets] C[Controller Type] P[Path String] RI[RouteInfo] MC --> M MC --> FR FR --> C FR --> P FR --> RI
Usage
tsimport { MiddlewareConfiguration, RouteInfo } from '@nestjs/common';
class LoggerMiddleware {
use(req: Request, res: Response, next: () => void) {
console.log(`${req.method} ${req.url}`);
next();
}
}
const configuration: MiddlewareConfiguration = {
middleware: LoggerMiddleware,
forRoutes: [
'users',
{ path: 'admin', method: RequestMethod.ALL } as RouteInfo,
],
};
AI Coding Instructions
- Set
middlewareto the middleware class, function, or compatible middleware value expected by the consuming configuration API. - Use
forRoutesto target controllers, route path strings, orRouteInfoobjects with explicit HTTP method matching. - Prefer
RouteInfowhen middleware should apply only to a specific HTTP method rather than every method for a path. - Ensure route strings and controller references match the routes registered by the application module.
- Keep middleware configuration close to module setup so route bindings remain discoverable and maintainable.
Used by
3 references from 3 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.
Imported by (3)
MiddlewareBuilder—packages/core/middleware/builder.ts:17moduleName—packages/core/middleware/container.ts:68handler—packages/core/middleware/middleware-module.ts:287
Was this page helpful?