Bird
0
0

Identify the error in this functional middleware code:

medium📝 Debug Q6 of 15
NestJS - Middleware
Identify the error in this functional middleware code:
function check(req, res, next) {
  if (!req.user) {
    res.status(401).send('Unauthorized');
  }
  next();
}
AMissing req, res parameters
Bnext() is called even after sending a response
Cres.status should be res.sendStatus
DMiddleware must be a class
Step-by-Step Solution
Solution:
  1. Step 1: Check response flow

    If req.user is missing, the middleware sends a 401 response.
  2. Step 2: Analyze next() call

    Calling next() after sending a response causes the next middleware or handler to run unnecessarily.
  3. Final Answer:

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

    Do not call next() after sending response [OK]
Quick Trick: Return or skip next() after sending response [OK]
Common Mistakes:
  • Calling next() after res.send()
  • Confusing res.status with res.sendStatus
  • Thinking middleware must be class-based

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes