Bird
0
0

Given this middleware code, what will be logged for every request?

medium📝 component behavior Q13 of 15
NestJS - Middleware
Given this middleware code, what will be logged for every request?
export class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: Function) {
    console.log(`Request to: ${req.url}`);
    next();
  }
}

// Applied globally with consumer.apply(LoggerMiddleware).forRoutes('*')
ALogs the HTTP method only
BLogs the response status code
CLogs nothing because next() is missing
DLogs the URL of every incoming request
Step-by-Step Solution
Solution:
  1. Step 1: Analyze middleware code

    The middleware logs the string 'Request to:' plus the request URL from req.url.
  2. Step 2: Understand global application

    Since applied globally, this log happens on every request URL received by the app.
  3. Final Answer:

    Logs the URL of every incoming request -> Option D
  4. Quick Check:

    LoggerMiddleware logs req.url every request [OK]
Quick Trick: Look for console.log inside middleware use() method [OK]
Common Mistakes:
  • Assuming HTTP method is logged instead of URL
  • Forgetting next() causes no further processing
  • Thinking response status is logged

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes