Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Order of Middleware Execution in NestJS
Given the following NestJS middleware setup, what will be the order of console logs when a request hits the '/test' route?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common'; @Injectable() class FirstMiddleware implements NestMiddleware { use(req, res, next) { console.log('First'); next(); } } @Injectable() class SecondMiddleware implements NestMiddleware { use(req, res, next) { console.log('Second'); next(); } } // In AppModule export class AppModule { configure(consumer) { consumer .apply(FirstMiddleware, SecondMiddleware) .forRoutes('/test'); } }
Attempts:
2 left
💡 Hint
Middleware are executed in the order they are applied in the consumer.
✗ Incorrect
In NestJS, middleware functions run in the order they are passed to the apply() method. Here, FirstMiddleware runs before SecondMiddleware, so 'First' logs before 'Second'.
❓ lifecycle
intermediate2:00remaining
Middleware Execution with next() Skipped
What happens if a middleware in NestJS does NOT call next() in its use() method?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common'; @Injectable() class BlockMiddleware implements NestMiddleware { use(req, res, next) { res.status(403).send('Blocked'); // next() is NOT called } } // Applied as the only middleware on '/block' route
Attempts:
2 left
💡 Hint
Calling next() passes control to the next middleware or handler.
✗ Incorrect
If next() is not called, the middleware chain stops. Here, the middleware sends a response and does not call next(), so no other middleware or handlers run.
🔧 Debug
advanced2:00remaining
Unexpected Middleware Execution Order
You applied two middleware in this order: AuthMiddleware, LoggerMiddleware. But the logs show LoggerMiddleware runs before AuthMiddleware. What is the most likely cause?
NestJS
consumer
.apply(AuthMiddleware, LoggerMiddleware)
.forRoutes('*');Attempts:
2 left
💡 Hint
Global middleware run before route-specific middleware.
✗ Incorrect
If LoggerMiddleware was registered globally before, it runs before route-specific middleware like AuthMiddleware, regardless of apply() order.
📝 Syntax
advanced2:00remaining
Correct Middleware Application Syntax
Which option correctly applies two middleware to the '/api' route in NestJS?
Attempts:
2 left
💡 Hint
apply() accepts middleware as separate arguments, not as an array.
✗ Incorrect
The apply() method takes middleware as separate arguments. Option C passes an array which is invalid. Option C chains apply() incorrectly. Option C uses a non-existent method 'use'.
❓ state_output
expert3:00remaining
Middleware Modifying Request Object
Consider two middleware applied in order: AddUserMiddleware adds req.user = {id: 1}, then LogUserMiddleware logs req.user. What will LogUserMiddleware output?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common'; @Injectable() class AddUserMiddleware implements NestMiddleware { use(req, res, next) { req.user = { id: 1 }; next(); } } @Injectable() class LogUserMiddleware implements NestMiddleware { use(req, res, next) { console.log(req.user); next(); } } // Applied as consumer.apply(AddUserMiddleware, LogUserMiddleware).forRoutes('*');
Attempts:
2 left
💡 Hint
Middleware run in order and share the same request object.
✗ Incorrect
AddUserMiddleware adds a user property to req before calling next(). LogUserMiddleware runs after and logs req.user, which is { id: 1 }.