NestJS - MiddlewareHow can NestJS middleware add custom properties to the request object so that downstream handlers can access them?ABy attaching new properties directly to the req object inside middlewareBBy modifying the response object instead of the requestCBy returning a new request object from middlewareDBy calling next() with additional argumentsCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand request object mutabilityThe req object is mutable and shared through middleware and handlers.Step 2: Add properties directlyMiddleware can add custom fields like req.user or req.timestamp.Final Answer:By attaching new properties directly to the req object inside middleware -> Option AQuick Check:Modify req object to pass data downstream [OK]Quick Trick: Add properties directly to req object [OK]Common Mistakes:Trying to replace req object entirelyModifying res instead of reqPassing data via next() arguments
Master "Middleware" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - Session-based authentication - Quiz 15hard Database with Prisma - Prisma migrations - Quiz 13medium Database with TypeORM - Entity definition - Quiz 2easy Database with TypeORM - Why TypeORM integrates seamlessly with NestJS - Quiz 9hard Interceptors - Response transformation - Quiz 8hard Middleware - Third-party middleware (cors, helmet) - Quiz 2easy Pipes - File validation pipe - Quiz 8hard Pipes - ValidationPipe in depth - Quiz 8hard Pipes - Default value pipe - Quiz 12easy Pipes - Default value pipe - Quiz 2easy