Challenge - 5 Problems
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What is the main purpose of interceptors in NestJS?
Interceptors in NestJS are often used to add cross-cutting logic. What does this mean in practice?
Attempts:
2 left
💡 Hint
Think about how interceptors can wrap around method calls to add shared behavior.
✗ Incorrect
Interceptors let you run code before and after method calls, so you can add shared features like logging or timing without changing each method.
❓ component_behavior
intermediate2:00remaining
How does an interceptor modify the response in NestJS?
Given an interceptor that logs the time taken by a method, what will be the output behavior?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; @Injectable() export class TimingInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { const start = Date.now(); return next.handle().pipe( tap(() => console.log(`Execution time: ${Date.now() - start}ms`)) ); } }
Attempts:
2 left
💡 Hint
Look at when the tap operator runs in the observable pipeline.
✗ Incorrect
The interceptor uses tap to log after the method finishes, but it does not modify the response data.
❓ lifecycle
advanced1:30remaining
When is the intercept method called in the NestJS request lifecycle?
At what point does the intercept method of a NestJS interceptor execute relative to controller method execution?
Attempts:
2 left
💡 Hint
Interceptors wrap around method calls like a sandwich.
✗ Incorrect
The intercept method runs before and after the controller method, allowing it to modify or observe the process.
📝 Syntax
advanced2:30remaining
Which interceptor code correctly adds a header to the HTTP response?
Choose the correct NestJS interceptor code that adds a custom header 'X-Custom' with value 'Hello' to the response.
Attempts:
2 left
💡 Hint
Check the correct method name to set a single header on the response object.
✗ Incorrect
The Express response object uses setHeader to add headers. Other methods like setHeaders do not exist or are incorrect.
🔧 Debug
expert3:00remaining
Why does this interceptor fail to log execution time?
Consider this interceptor code. It is supposed to log execution time but logs nothing. Why?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable } from 'rxjs'; @Injectable() export class SilentInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { const start = Date.now(); next.handle().subscribe(() => { console.log(`Execution time: ${Date.now() - start}ms`); }); return next.handle(); } }
Attempts:
2 left
💡 Hint
Think about how RxJS observables work and how side effects should be handled.
✗ Incorrect
Calling next.handle() twice creates two separate observables. The subscription inside intercept is ignored because the returned observable is different. Using tap inside the pipe ensures the side effect runs on the same observable.