Bird
0
0

What is wrong with this middleware code?

medium📝 Debug Q14 of 15
NestJS - Middleware
What is wrong with this middleware code?
class LoggerMiddleware {
  use(req, res, next) {
    console.log('Request received');
  }
}

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(LoggerMiddleware).forRoutes('users');
  }
}
AMissing call to next() inside LoggerMiddleware
BMiddlewareConsumer must be imported from '@nestjs/common' not '@nestjs/core'
CThe configure method should return a value
DNo semicolon after forRoutes call
Step-by-Step Solution
Solution:
  1. Step 1: Check middleware code

    The registration uses MiddlewareConsumer correctly, but examine the LoggerMiddleware class.
  2. Step 2: Identify the error

    The use method does not call next(), causing the request to hang without reaching the route handler.
  3. Final Answer:

    Missing call to next() inside LoggerMiddleware -> Option A
  4. Quick Check:

    Middleware must call next() to continue [OK]
Quick Trick: Always call next() inside middleware use() method [OK]
Common Mistakes:
  • Forgetting next() call causing request hang
  • Thinking configure must return a value
  • Confusing import sources for MiddlewareConsumer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes