Nestjs middleware pattern

From Intense-Visions/harness-engineering · View interactive version

Nestjs middleware pattern is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Apply NestMiddleware and functional middleware with consumer.forRoutes binding

How to use this skill

Skill file

NestJS Middleware Pattern

Apply NestMiddleware and functional middleware with consumer.forRoutes binding

When to Use

Instructions

  1. Class-based middleware — implements NestMiddleware:
@Injectable()
export class CorrelationIdMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction): void {
    req['correlationId'] = req.headers['x-correlation-id'] ?? randomUUID();
    res.setHeader('X-Correlation-ID', req['correlationId']);
    next();
  }
}
  1. Functional middleware — plain function (preferred when no DI needed):
export function requestLogger(req: Request, res: Response, next: NextFunction): void {
  console.log(`${req.method} ${req.url}`);
  next();
}
  1. Register in module via configure(consumer: MiddlewareConsumer):
@Module({ controllers: [UsersController] })
export class UsersModule implements NestModule {
  configure(consumer: MiddlewareConsumer): void {
    consumer.apply(CorrelationIdMiddleware).forRoutes(UsersController); // apply to entire controller

    consumer.apply(requestLogger).forRoutes({ path: 'users', method: RequestMethod.GET }); // specific method
  }
}
  1. Exclude routes:
consumer
  .apply(AuthMiddleware)
  .exclude({ path: 'auth/login', method: RequestMethod.POST })
  .forRoutes(UsersController);
  1. Global Express middleware — apply directly to the Express instance in main.ts (no NestJS wiring needed):
import helmet from 'helmet';
app.use(helmet());
app.use(compression());
  1. Chain multiple middleware: .apply(Middleware1, Middleware2).forRoutes(...) — they execute in order.

Details

NestJS middleware is equivalent to Express middleware — it receives (req, res, next) and must call next() or terminate the response. It runs before any NestJS-specific pipeline elements (guards, interceptors, pipes).

Middleware vs Guards: Middleware runs before routing. Guards run after routing with handler/controller metadata available. For authentication, use guards (they have ExecutionContext). For infrastructure concerns (logging, tracing, rate limiting), use middleware.

forRoutes() target types:

Class vs functional: Class middleware can inject providers (e.g., LoggerService, ConfigService) through the constructor. Functional middleware is simpler and slightly faster but cannot use DI. Choose functional when no injection is needed.

Execution order: Middleware declared in a module applies only to routes in that module (unless applied globally via app.use()). Import order in AppModule determines middleware execution order across modules.

Async middleware: Both class and functional middleware can be async. Errors thrown (or passed to next(err)) propagate to NestJS exception filters.

Source

https://docs.nestjs.com/middleware

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.