Bird
0
0

Which of the following is the correct syntax to apply a global middleware class named 'LoggerMiddleware' in NestJS?

easy📝 Syntax Q3 of 15
NestJS - Middleware
Which of the following is the correct syntax to apply a global middleware class named 'LoggerMiddleware' in NestJS?
Aapp.use(new LoggerMiddleware())
Bapp.apply(LoggerMiddleware).forRoutes('*')
Capp.use(LoggerMiddleware)
Dapp.middleware(LoggerMiddleware)
Step-by-Step Solution
Solution:
  1. Step 1: Recall global middleware syntax

    Global middleware is applied by passing the middleware class to app.use().
  2. Step 2: Identify correct syntax

    Passing the class reference (LoggerMiddleware) without instantiation is correct.
  3. Final Answer:

    app.use(LoggerMiddleware) -> Option C
  4. Quick Check:

    Global middleware syntax = app.use(ClassName) [OK]
Quick Trick: Pass middleware class, not instance, to app.use() [OK]
Common Mistakes:
  • Instantiating middleware with new inside app.use()
  • Using app.apply() which is for modules
  • Using non-existent app.middleware() method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes