Bird
0
0

Why does this middleware cause a runtime error?

medium📝 Debug Q7 of 15
NestJS - Middleware
Why does this middleware cause a runtime error?
@Injectable()
export class SampleMiddleware implements NestMiddleware {
  use(req: Request, res: Response) {
    console.log('Middleware running');
  }
}
AIncorrect class name
BMissing next parameter in use() method
CMissing @Controller decorator
Duse() method should be async
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature

    Middleware use() method must accept three parameters: req, res, and next.
  2. Step 2: Identify missing parameter

    The next parameter is missing, causing runtime errors when next() is called internally.
  3. Final Answer:

    Missing next parameter in use() method -> Option B
  4. Quick Check:

    use() needs req, res, next parameters [OK]
Quick Trick: Middleware use() must have three parameters: req, res, next [OK]
Common Mistakes:
  • Omitting next parameter
  • Confusing middleware with controllers
  • Assuming async is required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes