Bird
0
0

Which of the following is the correct syntax for a functional middleware in NestJS?

easy📝 Syntax Q3 of 15
NestJS - Middleware
Which of the following is the correct syntax for a functional middleware in NestJS?
Aconst logger = () => { console.log('Request'); }
Bclass Logger { use(req, res, next) { console.log('Request'); next(); } }
Cfunction logger(req, res, next) { console.log('Request'); next(); }
Dfunction logger(req, res) { console.log('Request'); }
Step-by-Step Solution
Solution:
  1. Step 1: Check function parameters

    Functional middleware must accept three parameters: req, res, and next.
  2. Step 2: Verify function calls next()

    Middleware must call next() to pass control; function logger(req, res, next) { console.log('Request'); next(); } does this correctly.
  3. Final Answer:

    function logger(req, res, next) { console.log('Request'); next(); } -> Option C
  4. Quick Check:

    Correct middleware syntax = function logger(req, res, next) { console.log('Request'); next(); } [OK]
Quick Trick: Middleware needs req, res, next and must call next() [OK]
Common Mistakes:
  • Omitting next parameter
  • Not calling next() inside middleware
  • Using arrow function without parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes