What is a cross-cutting concern in software development?
A cross-cutting concern is a part of a program that affects multiple components, like logging, security, or error handling, which is separate from the main business logic.
Click to reveal answer
beginner
How do interceptors in NestJS help with cross-cutting logic?
Interceptors let you run code before and after method execution, so you can add shared logic like logging or transforming responses without changing each method.
Click to reveal answer
beginner
Give an example of cross-cutting logic that an interceptor might add.
An interceptor can add logging to record when a request starts and ends, or it can modify the response data before sending it back to the client.
Click to reveal answer
intermediate
Why is it better to use interceptors for cross-cutting logic instead of adding code in every method?
Using interceptors keeps your code clean and DRY (Don't Repeat Yourself) by centralizing shared logic, making it easier to maintain and update.
Click to reveal answer
intermediate
What lifecycle phases can NestJS interceptors hook into?
Interceptors can run code before the method execution, after the method returns, and even handle errors or transform the result.
Click to reveal answer
What is the main purpose of an interceptor in NestJS?
ATo handle HTTP routing
BTo define database models
CTo add shared logic before and after method calls
DTo create user interfaces
✗ Incorrect
Interceptors add shared logic around method execution, such as logging or transforming responses.
Which of these is an example of cross-cutting logic?
ACalculating a user's age
BLogging every request
CRendering a page layout
DSaving user data
✗ Incorrect
Logging affects many parts of an app and is a typical cross-cutting concern.
Why should you avoid adding cross-cutting logic directly inside each method?
AIt makes code repetitive and harder to maintain
BIt improves performance
CIt reduces code size
DIt simplifies debugging
✗ Incorrect
Repeating the same code in many places makes maintenance difficult and error-prone.
When can a NestJS interceptor run its code?
AOnly during app startup
BOnly after method execution
COnly before method execution
DBefore and after method execution
✗ Incorrect
Interceptors can run code both before and after the method runs.
Which NestJS feature is best suited for adding cross-cutting logic?
AInterceptors
BControllers
CModules
DServices
✗ Incorrect
Interceptors are designed to add shared logic around method calls.
Explain in your own words why interceptors are useful for adding cross-cutting logic in NestJS.
Think about how you can add the same behavior to many parts of your app without copying code.
You got /4 concepts.
Describe a real-life situation where using an interceptor for cross-cutting logic would help keep your code clean.
Imagine you want to track every request time or change all responses in the same way.
You got /4 concepts.
Practice
(1/5)
1. 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?
easy
A. They add shared logic before and after main code, like logging or timing.
B. They replace controllers to handle HTTP requests directly.
C. They store data permanently in the database.
D. They generate frontend UI components automatically.
Solution
Step 1: Understand the role of interceptors
Interceptors run code before and after the main handler, allowing shared tasks like logging or error handling.
Step 2: Compare with other options
Controllers handle requests, databases store data, and UI components are unrelated to interceptors.
Final Answer:
They add shared logic before and after main code, like logging or timing. -> Option A
Quick Check:
Interceptors add shared logic = A [OK]
Hint: Interceptors wrap main code to add shared tasks [OK]
B. The console.log statement is after next.handle(), so it never runs.
C. The class is missing the @Controller() decorator.
D. The intercept method does not return the observable from next.handle().
Solution
Step 1: Check the intercept method return
The intercept method must return the observable from next.handle() to continue the request flow.
Step 2: Identify missing return
Here, next.handle() is called but its result is not returned, so the request does not proceed and logs may not appear.
Final Answer:
The intercept method does not return the observable from next.handle(). -> Option D
Quick Check:
Missing return next.handle() = D [OK]
Hint: Always return next.handle() observable in intercept() [OK]
Common Mistakes:
Forgetting to return next.handle()
Misplacing console.log after next.handle()
Confusing decorators for interceptors
5. You want to measure the time taken by any request handler in your NestJS app. Which interceptor approach correctly adds this cross-cutting timing logic?