Bird
0
0

Identify the error in this global middleware setup:

medium📝 Debug Q14 of 15
NestJS - Middleware
Identify the error in this global middleware setup:
export class AuthMiddleware implements NestMiddleware {
  use(req: Request, res: Response) {
    if (!req.headers.authorization) {
      res.status(401).send('Unauthorized');
    }
  }
}

// Applied with consumer.apply(AuthMiddleware).forRoutes('*')
AMissing call to next() to continue request processing
BMiddleware should not check headers
CforRoutes('*') cannot be used for global middleware
Dres.status(401) is invalid syntax
Step-by-Step Solution
Solution:
  1. Step 1: Check middleware method signature

    The use method lacks the next parameter and does not call next(), so request stops here.
  2. Step 2: Understand middleware flow

    Without calling next(), the request will hang or not proceed to route handlers after middleware.
  3. Final Answer:

    Missing call to next() to continue request processing -> Option A
  4. Quick Check:

    Middleware must call next() to proceed [OK]
Quick Trick: Always call next() in middleware to continue [OK]
Common Mistakes:
  • Omitting next() call in middleware
  • Misunderstanding middleware flow control
  • Thinking forRoutes('*') is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes