Consider a NestJS global middleware that adds a property requestTime to the request object. What will be the output of the controller logging req.requestTime?
import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response, NextFunction } from 'express'; @Injectable() export class LoggerMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { req['requestTime'] = '2024-06-01T12:00:00Z'; next(); } } // In main.ts app.use(new LoggerMiddleware()); // In controller @Get() getHello(@Req() req: Request): string { console.log(req['requestTime']); return 'Hello World'; }
Think about how middleware can add properties to the request object before it reaches the controller.
Global middleware runs before the controller and can add properties to the request object. These properties are accessible in the controller, so the logged value is the string set in middleware.
At what point in the request lifecycle does a global middleware run in a NestJS application?
Think about middleware as the first step in handling a request.
Global middleware runs before any controller or route handler. It processes the request early, allowing modifications or logging before the main logic.
Given the following code, why does the middleware not run for incoming requests?
import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response, NextFunction } from 'express'; @Injectable() export class MyMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { console.log('Middleware running'); next(); } } // In main.ts async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(new MyMiddleware()); await app.listen(3000); } bootstrap();
Check how middleware is passed to app.use().
In NestJS, app.use() expects a function or middleware instance, not the class itself. Passing the class without instantiation means the middleware does not run.
Choose the correct syntax to apply a global middleware in NestJS version 9 or later.
Remember how Express middleware is applied in NestJS.
In NestJS, global middleware is applied using app.use() with an instance or function. useGlobalMiddleware or applyGlobalMiddleware are not valid methods.
Two global middleware run in sequence. The first sets header X-First to one. The second sets X-Second to two. What headers will the client receive?
import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response, NextFunction } from 'express'; @Injectable() export class FirstMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { res.setHeader('X-First', 'one'); next(); } } @Injectable() export class SecondMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { res.setHeader('X-Second', 'two'); next(); } } // In main.ts app.use(new FirstMiddleware()); app.use(new SecondMiddleware()); // Controller returns 'OK'
Think about how HTTP headers are set and merged in Express response.
Each middleware sets a different header. Express merges headers, so both headers are sent to the client.