Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why does middleware run before route handlers in NestJS?
In NestJS, middleware functions run before route handlers. Why is this order important?
Attempts:
2 left
💡 Hint
Think about what middleware can do with the request before the main code runs.
✗ Incorrect
Middleware runs before handlers so it can inspect, modify, or validate the request. This ensures handlers get the right data or that unauthorized requests are blocked early.
❓ component_behavior
intermediate2:00remaining
What happens if middleware modifies the request object?
Consider this NestJS middleware that adds a property to the request object. What will the route handler see?
NestJS
export function addUserId(req, res, next) {
req.userId = 42;
next();
}
// Route handler
@Get('profile')
getProfile(@Req() req) {
return req.userId;
}Attempts:
2 left
💡 Hint
Middleware can add properties to the request object that handlers can use.
✗ Incorrect
Middleware runs first and can add or change properties on the request object. The handler then accesses these properties as part of the same request.
❓ lifecycle
advanced2:00remaining
Order of middleware and handlers in NestJS request lifecycle
Which sequence correctly shows the order of execution when a request comes in a NestJS app with middleware and a route handler?
Attempts:
2 left
💡 Hint
Middleware runs first and can modify request or response before handler runs.
✗ Incorrect
Middleware runs first (1), can modify request or response (4), then the route handler runs (2), and finally the response is sent (3).
📝 Syntax
advanced2:00remaining
Identify the correct middleware usage in NestJS
Which option correctly applies middleware to a route in NestJS?
Attempts:
2 left
💡 Hint
Check the official NestJS way to apply middleware in main.ts or module.
✗ Incorrect
In NestJS, middleware is applied using app.use(path, middleware). Other options are invalid methods.
🔧 Debug
expert3:00remaining
Why does this middleware not run before the handler?
Given this NestJS setup, why does the middleware not run before the route handler?
NestJS
export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer .apply(loggingMiddleware) .forRoutes('users'); } } @Get('/users') getUsers() { return ['Alice', 'Bob']; }
Attempts:
2 left
💡 Hint
Check how paths are matched in middleware configuration.
✗ Incorrect
Middleware paths must match routes exactly including the leading slash. 'users' does not match '/users', so middleware is skipped.