Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Middleware effect on route response
Given this NestJS middleware that adds a header, what will be the response headers when accessing the
/hello route?NestJS
import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response, NextFunction } from 'express'; @Injectable() export class HeaderMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { res.setHeader('X-Custom-Header', 'NestJS'); next(); } } // In AppModule import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common'; import { HeaderMiddleware } from './header.middleware'; import { HelloController } from './hello.controller'; @Module({ controllers: [HelloController], }) export class AppModule { configure(consumer: MiddlewareConsumer) { consumer .apply(HeaderMiddleware) .forRoutes({ path: 'hello', method: RequestMethod.GET }); } } // HelloController import { Controller, Get } from '@nestjs/common'; @Controller('hello') export class HelloController { @Get() getHello() { return 'Hello World'; } }
Attempts:
2 left
💡 Hint
Middleware runs before the route handler and can modify the response.
✗ Incorrect
The middleware sets the header 'X-Custom-Header' to 'NestJS' before calling next(), so the route response includes this header.
📝 Syntax
intermediate2:00remaining
Correct middleware application syntax
Which option correctly applies a middleware to all POST routes in NestJS?
NestJS
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common'; import { LoggerMiddleware } from './logger.middleware'; @Module({}) export class AppModule { configure(consumer: MiddlewareConsumer) { // Apply middleware here } }
Attempts:
2 left
💡 Hint
Use RequestMethod enum for HTTP methods.
✗ Incorrect
The correct syntax uses forRoutes with an object specifying path and method using RequestMethod.POST enum.
🔧 Debug
advanced2:00remaining
Middleware not running on expected route
Why does this middleware not run when accessing
/api/users even though it is applied to /api/*?NestJS
consumer.apply(AuthMiddleware).forRoutes('api/*');Attempts:
2 left
💡 Hint
Check how forRoutes accepts paths.
✗ Incorrect
forRoutes does not support wildcard strings. It expects exact paths, controller classes, or RequestMethod objects.
❓ state_output
advanced2:00remaining
Order of middleware execution
Given two middlewares applied in this order, what will be the console output when accessing
/test?NestJS
consumer .apply(FirstMiddleware, SecondMiddleware) .forRoutes('test'); // FirstMiddleware logs 'First start' before next() and 'First end' after next() // SecondMiddleware logs 'Second start' before next() and 'Second end' after next()
Attempts:
2 left
💡 Hint
Middleware calls next() to pass control to the next middleware.
✗ Incorrect
Middlewares run in the order applied. Each logs before next(), then next middleware runs, then logs after next().
🧠 Conceptual
expert3:00remaining
Middleware scope and lifecycle
Which statement about NestJS middleware is true?
Attempts:
2 left
💡 Hint
Think about what middleware does in the request lifecycle.
✗ Incorrect
Middleware runs before route handlers and can modify request and response objects. It is instantiated once and shared, not per request.