Bird
0
0

You want to apply the same interceptor logic to multiple controllers without repeating code. What is the best approach?

hard📝 Application Q9 of 15
NestJS - Interceptors
You 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 module
BCopy and paste the interceptor code in each controller
CWrite the interceptor logic inside each controller method
DUse middleware instead of interceptors
Step-by-Step Solution
Solution:
  1. Step 1: Understand global interceptor usage

    Global interceptors apply to all controllers automatically, avoiding repetition.
  2. Step 2: Compare with other options

    Copy-pasting or writing logic in each method is inefficient; middleware serves different purpose.
  3. Final Answer:

    Create a global interceptor and apply it in the main app module -> Option A
  4. Quick Check:

    Global interceptor = reuse logic app-wide [OK]
Quick Trick: Use global interceptors for shared logic across controllers [OK]
Common Mistakes:
  • Duplicating code in controllers
  • Confusing middleware with interceptors
  • Writing logic inside each method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes