Bird
0
0

Why does this global middleware fail to execute properly in NestJS?

medium📝 Debug Q6 of 15
NestJS - Middleware
Why does this global middleware fail to execute properly in NestJS?
export class CheckMiddleware {
  use(req, res) {
    console.log('Checking request');
    next();
  }
}

app.use(CheckMiddleware);
AMiddleware must be applied inside a module, not in main.ts
BMiddleware classes cannot be used with app.use()
CThe console.log statement is incorrect syntax
DThe middleware's use method is missing the next parameter
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature

    The use method must accept three parameters: req, res, next.
  2. Step 2: Identify missing parameter

    Here, next is called but not declared as a parameter, causing a runtime error.
  3. Final Answer:

    The middleware's use method is missing the next parameter -> Option D
  4. Quick Check:

    Middleware must declare next to call it [OK]
Quick Trick: Always include next parameter in middleware use method [OK]
Common Mistakes:
  • Forgetting to declare next parameter
  • Assuming app.use can't accept classes
  • Misplacing middleware application

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes