NestJS - InterceptorsYou want to apply the same interceptor logic to multiple controllers without repeating code. What is the best approach?ACreate a global interceptor and apply it in the main app moduleBCopy and paste the interceptor code in each controllerCWrite the interceptor logic inside each controller methodDUse middleware instead of interceptorsCheck Answer
Step-by-Step SolutionSolution:Step 1: Understand global interceptor usageGlobal interceptors apply to all controllers automatically, avoiding repetition.Step 2: Compare with other optionsCopy-pasting or writing logic in each method is inefficient; middleware serves different purpose.Final Answer:Create a global interceptor and apply it in the main app module -> Option AQuick Check:Global interceptor = reuse logic app-wide [OK]Quick Trick: Use global interceptors for shared logic across controllers [OK]Common Mistakes:Duplicating code in controllersConfusing middleware with interceptorsWriting logic inside each method
Master "Interceptors" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - Token generation and validation - Quiz 5medium Database with Prisma - Prisma setup in NestJS - Quiz 14medium Database with Prisma - CRUD with Prisma - Quiz 8hard Database with Prisma - Relations in Prisma - Quiz 2easy Guards - Combining multiple guards - Quiz 9hard Guards - Guard binding levels - Quiz 13medium Interceptors - Response transformation - Quiz 8hard Middleware - Creating middleware - Quiz 12easy Pipes - Pipe binding (parameter, method, controller, global) - Quiz 3easy Pipes - ValidationPipe in depth - Quiz 13medium