Recall & Review
beginner
What is a Timeout Interceptor in NestJS?
A Timeout Interceptor in NestJS is a piece of code that automatically stops a request if it takes longer than a set time. It helps keep the app responsive by avoiding long waits.
Click to reveal answer
intermediate
How do you apply a Timeout Interceptor globally in NestJS?
You apply it globally by using
app.useGlobalInterceptors(new TimeoutInterceptor()) in the main bootstrap file. This makes all requests use the timeout rule.Click to reveal answer
intermediate
Which RxJS operator is commonly used inside a Timeout Interceptor to enforce the timeout?
The
timeout operator from RxJS is used. It throws an error if the observable does not emit a value within the specified time.Click to reveal answer
beginner
What happens if a request exceeds the timeout set by the Timeout Interceptor?
The interceptor throws a
TimeoutError, which usually results in a 408 Request Timeout response sent back to the client.Click to reveal answer
advanced
Show a simple example of a Timeout Interceptor class in NestJS.import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { timeout, catchError } from 'rxjs/operators';
@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
timeout(5000), // 5 seconds timeout
catchError(err => {
if (err instanceof TimeoutError) {
return throwError(() => new RequestTimeoutException());
}
return throwError(() => err);
}),
);
}
}Click to reveal answer
What is the main purpose of a Timeout Interceptor in NestJS?
✗ Incorrect
Timeout Interceptors stop requests that exceed a set time to keep the app responsive.
Which RxJS operator is used to enforce a timeout in a NestJS interceptor?
✗ Incorrect
The timeout operator throws an error if the observable takes too long.
What exception is typically thrown when a timeout occurs in NestJS?
✗ Incorrect
RequestTimeoutException signals that the request took too long and was stopped.
How do you apply a Timeout Interceptor to all routes in a NestJS app?
✗ Incorrect
Using app.useGlobalInterceptors applies the interceptor app-wide.
If a Timeout Interceptor is set to 3 seconds, what happens if a request takes 5 seconds?
✗ Incorrect
The interceptor cancels the request and returns a timeout error.
Explain how a Timeout Interceptor works in NestJS and why it is useful.
Think about how to handle slow requests in a web app.
You got /4 concepts.
Describe how to create and apply a Timeout Interceptor globally in a NestJS application.
Focus on the class structure and global setup.
You got /4 concepts.