0
0
NestJSframework~10 mins

Creating middleware in NestJS - Interactive Practice

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

Complete the code to import the MiddlewareConsumer from NestJS.

NestJS
import { [1] } from '@nestjs/common';
Drag options to blanks, or click blank then click option'
AModule
BController
CMiddlewareConsumer
DInjectable
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Controller instead of MiddlewareConsumer.
Forgetting to import MiddlewareConsumer.
2fill in blank
medium

Complete the code to implement the NestJS middleware interface.

NestJS
export class LoggerMiddleware implements [1] {
Drag options to blanks, or click blank then click option'
ANestMiddleware
BPipeTransform
CExceptionFilter
DCanActivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using PipeTransform instead of NestMiddleware.
Not implementing any interface.
3fill in blank
hard

Fix the error in the middleware method signature.

NestJS
use(req: Request, res: Response, [1]) {
Drag options to blanks, or click blank then click option'
Anext: () => void
Bnext: NextFunction
Cnext: Function
Dnext: any
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect type like () => void or any.
Omitting the next parameter.
4fill in blank
hard

Fill both blanks to apply middleware to all routes in a module.

NestJS
configure(consumer: [1]) {
  consumer
    .apply(LoggerMiddleware)
    .[2]('*');
}
Drag options to blanks, or click blank then click option'
AMiddlewareConsumer
BModuleRef
CforRoutes
Duse
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type in configure method.
Using 'use' instead of 'forRoutes' to apply middleware.
5fill in blank
hard

Fill all three blanks to create a middleware that logs the request method and URL, then calls next.

NestJS
export class LoggerMiddleware implements NestMiddleware {
  use(req: [1], res: [2], [3]) {
    console.log(`${req.method} ${req.url}`);
    next();
  }
}
Drag options to blanks, or click blank then click option'
ARequest
BResponse
Cnext: NextFunction
DHttpException
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect types for req or res.
Omitting the next parameter or using wrong type.