0
0
NestJSframework~10 mins

Logging interceptor in NestJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the necessary NestJS module for interceptors.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AInjectable
BNestInterceptor
CController
DModule
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Controller instead of NestInterceptor
Using Injectable instead of NestInterceptor
2fill in blank
medium

Complete the code to implement the NestJS interceptor interface.

NestJS
export class LoggingInterceptor implements [1] {
Drag options to blanks, or click blank then click option'
ANestInterceptor
BCanActivate
CExceptionFilter
DPipeTransform
Attempts:
3 left
💡 Hint
Common Mistakes
Implementing PipeTransform instead of NestInterceptor
Implementing ExceptionFilter instead of NestInterceptor
3fill in blank
hard

Fix the error in the method signature to correctly intercept the request.

NestJS
intercept(context: ExecutionContext, [1]: CallHandler): Observable<any> {
Drag options to blanks, or click blank then click option'
Acall
Bhandler
Cnext
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'handler' or 'call' instead of 'next'
Using 'request' which is not the correct parameter here
4fill in blank
hard

Fill both blanks to log the request method and URL inside the interceptor.

NestJS
const request = context.switchToHttp().getRequest();
console.log(`Request: ${request.[1] ${request.[2]`);
Drag options to blanks, or click blank then click option'
Amethod
Burl
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'body' or 'headers' instead of 'method' or 'url'
Mixing the order of method and url
5fill in blank
hard

Fill all three blanks to complete the interceptor's return statement that logs after the response is sent.

NestJS
return next.handle().pipe(tap(() => {
  console.log(`Response sent for ${context.switchToHttp().getRequest().[1] ${context.switchToHttp().getRequest().[2]`);
  console.log(`Execution time: ${Date.now() - [3]ms`);
}));
Drag options to blanks, or click blank then click option'
Amethod
Burl
CstartTime
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'headers' instead of 'method' or 'url'
Using 'Date.now()' directly instead of a stored start time