0
0
NestJSframework~10 mins

Functional middleware 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 create a functional middleware that logs the request method.

NestJS
export function loggerMiddleware(req, res, next) {
  console.log(req.[1]);
  next();
}
Drag options to blanks, or click blank then click option'
Amethod
Burl
Cbody
Dheaders
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.url instead of req.method
Forgetting to call next() to continue middleware chain
2fill in blank
medium

Complete the code to apply the functional middleware globally in a NestJS app.

NestJS
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { loggerMiddleware } from './logger.middleware';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.[1](loggerMiddleware);
  await app.listen(3000);
}
bootstrap();
Drag options to blanks, or click blank then click option'
AuseGlobalGuards
Buse
CuseGlobalInterceptors
DuseGlobalFilters
Attempts:
3 left
💡 Hint
Common Mistakes
Using useGlobalGuards instead of use
Not importing the middleware function correctly
3fill in blank
hard

Fix the error in the functional middleware to correctly handle asynchronous operations.

NestJS
export async function asyncLoggerMiddleware(req, res, [1]) {
  await new Promise(resolve => setTimeout(resolve, 100));
  console.log('Async log:', req.method);
  next();
}
Drag options to blanks, or click blank then click option'
Anext
Bdone
Ccallback
Dhandle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name for the third argument
Not calling next() after async code
4fill in blank
hard

Fill both blanks to create a functional middleware that adds a custom header and calls next.

NestJS
export function customHeaderMiddleware(req, res, [1]) {
  res.setHeader('[2]', 'NestJS');
  next();
}
Drag options to blanks, or click blank then click option'
Anext
Bdone
CX-Custom-Header
DContent-Type
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name instead of next
Setting standard headers instead of custom ones
5fill in blank
hard

Fill all three blanks to create a functional middleware that logs the URL, sets a header, and calls next.

NestJS
export function fullMiddleware(req, res, [1]) {
  console.log('URL:', req.[2]);
  res.setHeader('[3]', 'Active');
  next();
}
Drag options to blanks, or click blank then click option'
Anext
Burl
CX-Status
Dmethod
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.method instead of req.url for logging URL
Forgetting to call next()