Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this NestJS middleware logging example?
Consider this NestJS middleware that logs the request method and URL. What will be logged when a GET request is made to '/users'?
NestJS
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) { console.log(`${req.method} ${req.url}`); next(); } }
Attempts:
2 left
💡 Hint
Look at how req.method and req.url are used inside the middleware.
✗ Incorrect
The middleware logs the HTTP method and URL of the incoming request. For a GET request to '/users', it logs 'GET /users'.
❓ lifecycle
intermediate1:30remaining
When is NestJS middleware executed in the request lifecycle?
At what point in the request lifecycle does NestJS middleware run?
Attempts:
2 left
💡 Hint
Middleware typically processes requests before controllers handle them.
✗ Incorrect
NestJS middleware runs before the route handler to process or modify the request or response.
📝 Syntax
advanced2:30remaining
Which option correctly applies middleware to a specific route in NestJS?
You want to apply a middleware only to the '/api/products' route. Which code snippet correctly does this in NestJS?
NestJS
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { LoggerMiddleware } from './logger.middleware'; @Module({}) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { // Choose the correct option below } }
Attempts:
2 left
💡 Hint
Check the NestJS MiddlewareConsumer API for the correct method names.
✗ Incorrect
The correct method to apply middleware is 'apply', and the method to specify routes is 'forRoutes'.
🔧 Debug
advanced2:00remaining
Why does this NestJS middleware not log anything?
This middleware is supposed to log request info but logs nothing. What is the cause?
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common'; import { Request, Response, NextFunction } from 'express'; @Injectable() export class SilentMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { // Missing console.log here next(); } }
Attempts:
2 left
💡 Hint
Look inside the use method for logging statements.
✗ Incorrect
The middleware function does not have any console.log or logging code, so it produces no output.
🧠 Conceptual
expert2:00remaining
What happens if NestJS middleware does not call next()?
In NestJS, what is the effect of middleware that does not call the next() function?
Attempts:
2 left
💡 Hint
Think about how middleware chains work in Express-based frameworks.
✗ Incorrect
Middleware must call next() to pass control; otherwise, the request stops and hangs.