Bird
0
0

Which of the following is the correct way to define a middleware class in NestJS?

easy📝 Syntax Q12 of 15
NestJS - Middleware
Which of the following is the correct way to define a middleware class in NestJS?
Aconst MyMiddleware = (req, res, next) => { next(); }
Bclass MyMiddleware { handle(req, res, next) { next(); } }
Cfunction MyMiddleware(req, res, next) { next(); }
Dclass MyMiddleware { use(req, res, next) { next(); } }
Step-by-Step Solution
Solution:
  1. Step 1: Recall NestJS middleware pattern

    Middleware must be a class with a method named use that takes req, res, next.
  2. Step 2: Check options

    Only class MyMiddleware { use(req, res, next) { next(); } } defines a class with a use method correctly. class MyMiddleware { handle(req, res, next) { next(); } } uses handle, which is invalid. Options C and D define functions, not classes.
  3. Final Answer:

    class MyMiddleware { use(req, res, next) { next(); } } -> Option D
  4. Quick Check:

    Middleware class must have use() method [OK]
Quick Trick: Middleware class must have a use() method [OK]
Common Mistakes:
  • Using method name other than use
  • Defining middleware as a function instead of class
  • Forgetting to call next() inside use

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes