Bird
0
0

What is the main reason to use interceptors in NestJS?

easy📝 Conceptual Q11 of 15
NestJS - Interceptors
What is the main reason to use interceptors in NestJS?
Interceptors help to add logic that applies to many parts of the app without repeating code. Why is this useful?
AThey add shared logic before and after main code, like logging or timing.
BThey replace controllers to handle HTTP requests directly.
CThey store data permanently in the database.
DThey generate frontend UI components automatically.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the role of interceptors

    Interceptors run code before and after the main handler, allowing shared tasks like logging or error handling.
  2. Step 2: Compare with other options

    Controllers handle requests, databases store data, and UI components are unrelated to interceptors.
  3. Final Answer:

    They add shared logic before and after main code, like logging or timing. -> Option A
  4. Quick Check:

    Interceptors add shared logic = A [OK]
Quick Trick: Interceptors wrap main code to add shared tasks [OK]
Common Mistakes:
  • Thinking interceptors handle HTTP requests directly
  • Confusing interceptors with database or UI roles
  • Assuming interceptors replace controllers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes