Bird
0
0

Identify the error in this middleware implementation:

medium📝 Debug Q6 of 15
NestJS - Middleware
Identify the error in this middleware implementation:
export class AuthMiddleware implements NestMiddleware {
  use(req, res, next) {
    if (!req.headers.authorization) {
      res.status(401).send('Unauthorized');
    }
    next();
  }
}
AIncorrect method name, should be handle()
BMissing @Injectable() decorator
Cnext() is called even after sending response
Dreq.headers is undefined
Step-by-Step Solution
Solution:
  1. Step 1: Analyze flow after sending response

    If res.status(401).send() is called, the response ends and next() should not be called.
  2. Step 2: Identify the error

    Calling next() after sending a response can cause errors or unexpected behavior.
  3. Final Answer:

    next() is called even after sending response -> Option C
  4. Quick Check:

    Do not call next() after response sent [OK]
Quick Trick: Stop calling next() after sending a response [OK]
Common Mistakes:
  • Calling next() after res.send()
  • Forgetting @Injectable() (optional but recommended)
  • Using wrong method name for middleware

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes