Challenge - 5 Problems
Logging Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
Output of a simple NestJS logging interceptor
Given the following NestJS logging interceptor code, what will be logged when a controller method returns 'Hello World'?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; import { Observable, tap } from 'rxjs'; @Injectable() export class LoggingInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler): Observable<any> { console.log('Before handler execution'); return next.handle().pipe( tap(() => console.log('After handler execution')) ); } }
Attempts:
2 left
💡 Hint
Think about when the tap operator runs in an RxJS observable pipeline.
✗ Incorrect
The interceptor logs a message before the handler runs, then uses tap to log after the handler completes.
🧠 Conceptual
intermediate1:00remaining
Purpose of a logging interceptor in NestJS
What is the main purpose of using a logging interceptor in a NestJS application?
Attempts:
2 left
💡 Hint
Think about what interceptors can do with requests and responses.
✗ Incorrect
Logging interceptors are used to capture and log information about requests and responses in one place.
❓ Configuration
advanced1:30remaining
Correct way to apply a global logging interceptor in NestJS
Which code snippet correctly applies a logging interceptor globally in a NestJS application?
NestJS
import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { LoggingInterceptor } from './logging.interceptor'; async function bootstrap() { const app = await NestFactory.create(AppModule); // Apply interceptor here await app.listen(3000); } bootstrap();
Attempts:
2 left
💡 Hint
Check the exact method name for applying global interceptors in NestJS.
✗ Incorrect
The correct method to apply global interceptors is useGlobalInterceptors.
❓ Troubleshoot
advanced2:00remaining
Why does the logging interceptor not log after handler execution?
A developer notices that their NestJS logging interceptor logs the message before the handler but never logs the message after the handler execution. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check the return statement in the intercept method.
✗ Incorrect
The interceptor must return the Observable from next.handle().pipe(...); otherwise, the framework does not subscribe to it, and operators like tap never execute.
🔀 Workflow
expert2:30remaining
Order of execution in multiple NestJS interceptors
Given two global interceptors, LoggingInterceptor and TimingInterceptor, applied in this order:
app.useGlobalInterceptors(new LoggingInterceptor(), new TimingInterceptor());
Which order will their 'before' and 'after' logs appear when a controller method is called?
Attempts:
2 left
💡 Hint
Think of interceptors like nested layers wrapping the controller method.
✗ Incorrect
Interceptors execute in the order they are applied before the handler, and in reverse order after the handler.