NestJS - InterceptorsYou want to create a LoggingInterceptor that logs request duration only if it exceeds 500ms. How would you implement this?AInside tap, check if duration > 500ms before loggingBUse a guard to block requests taking longer than 500msCSet a timeout of 500ms on next.handle() ObservableDLog all requests and filter logs externally for duration > 500msCheck Answer
Step-by-Step SolutionSolution:Step 1: Measure request duration inside interceptRecord start time before next.handle(), calculate duration in tap.Step 2: Add condition to log only if duration exceeds 500msUse if statement inside tap to log selectively.Final Answer:Inside tap, check if duration > 500ms before logging -> Option AQuick Check:Conditional logging done inside tap operator [OK]Quick Trick: Use conditional inside tap to log selectively [OK]Common Mistakes:Using guards for timing logicApplying timeout incorrectlyLogging all requests without filtering
Master "Interceptors" in NestJS9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallPerf
More NestJS Quizzes Authentication - JWT strategy - Quiz 2easy Database with Prisma - Why Prisma offers type-safe database access - Quiz 14medium Database with TypeORM - Relations (OneToMany, ManyToOne, ManyToMany) - Quiz 5medium Database with TypeORM - Relations (OneToMany, ManyToOne, ManyToMany) - Quiz 6medium Database with TypeORM - Migrations - Quiz 4medium Guards - Role-based guards - Quiz 7medium Guards - Combining multiple guards - Quiz 8hard Interceptors - Cache interceptor - Quiz 5medium Middleware - Functional middleware - Quiz 10hard Pipes - Pipe binding (parameter, method, controller, global) - Quiz 6medium