0
0
NestJSframework~20 mins

Why interceptors add cross-cutting logic in NestJS - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1: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?
AThey allow you to run code before and after method execution, affecting multiple parts of the app without changing each method.
BThey replace the main logic of a controller method with a new implementation.
CThey only handle errors thrown by controller methods.
DThey are used to define database models and schemas.
Attempts:
2 left
💡 Hint
Think about how interceptors can wrap around method calls to add shared behavior.
component_behavior
intermediate
2: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`))
    );
  }
}
AThe interceptor blocks the response and only logs the time without returning anything.
BThe interceptor changes the response to include the execution time as a new field.
CThe interceptor logs the time before the method starts and returns an error.
DThe interceptor logs the execution time after the method completes, then returns the original response unchanged.
Attempts:
2 left
💡 Hint
Look at when the tap operator runs in the observable pipeline.
lifecycle
advanced
1: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?
AOnly after the controller method has completed and sent the response.
BBefore the controller method is called and after it returns, wrapping the method execution.
COnly before the controller method is called, without access to the response.
DOnly when an exception is thrown inside the controller method.
Attempts:
2 left
💡 Hint
Interceptors wrap around method calls like a sandwich.
📝 Syntax
advanced
2: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.
A
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class HeaderInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const response = context.switchToHttp().getResponse();
    response.setHeader('X-Custom', 'Hello');
    return next.handle();
  }
}
B
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class HeaderInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const response = context.getResponse();
    response.headers['X-Custom'] = 'Hello';
    return next.handle();
  }
}
C
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class HeaderInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const response = context.switchToHttp().getResponse();
    response.header('X-Custom', 'Hello');
    return next.handle();
  }
}
D
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class HeaderInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable&lt;any&gt; {
    const response = context.switchToHttp().getResponse();
    response.setHeaders({'X-Custom': 'Hello'});
    return next.handle();
  }
}
Attempts:
2 left
💡 Hint
Check the correct method name to set a single header on the response object.
🔧 Debug
expert
3: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();
  }
}
AThe interceptor returns next.handle() before the subscription logs, so the log never executes.
BThe console.log is inside subscribe but next.handle() is not subscribed properly, so it never runs.
CThe interceptor should use tap operator instead of subscribe to log side effects correctly.
DCalling next.handle() twice causes the observable to run twice, but the first subscription logs nothing because it is not awaited.
Attempts:
2 left
💡 Hint
Think about how RxJS observables work and how side effects should be handled.