NestJS - MiddlewareWhich 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'); }Check Answer
Step-by-Step SolutionSolution:Step 1: Check function parametersFunctional middleware must accept three parameters: req, res, and next.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.Final Answer:function logger(req, res, next) { console.log('Request'); next(); } -> Option CQuick 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 parameterNot calling next() inside middlewareUsing arrow function without parameters
Master "Middleware" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - Protected routes with guards - Quiz 10hard Database with TypeORM - TypeORM module setup - Quiz 12easy Database with TypeORM - Relations (OneToMany, ManyToOne, ManyToMany) - Quiz 9hard Database with TypeORM - Migrations - Quiz 10hard Database with TypeORM - Query builder - Quiz 11easy Database with TypeORM - Migrations - Quiz 15hard Interceptors - Interceptor interface - Quiz 1easy Interceptors - Cache interceptor - Quiz 4medium Middleware - Why middleware processes requests before handlers - Quiz 12easy Middleware - Applying middleware to routes - Quiz 13medium