0
0
NestJSframework~20 mins

Functional middleware in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Functional Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this NestJS functional middleware?
Consider this functional middleware that logs request method and URL. What will be logged when a GET request is made to '/api/data'?
NestJS
import { Request, Response, NextFunction } from 'express';

export function logger(req: Request, res: Response, next: NextFunction) {
  console.log(`${req.method} ${req.url}`);
  next();
}
ALogs nothing and immediately sends a 404 response
BLogs: 'POST /api/data' and stops request processing
CThrows a runtime error because next() is missing
DLogs: 'GET /api/data' and passes control to next middleware
Attempts:
2 left
💡 Hint
Remember that next() passes control to the next middleware or route handler.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a functional middleware in NestJS?
Select the option that correctly defines a functional middleware that adds a custom header 'X-Custom' with value 'NestJS' to the response.
Aexport const customHeader = (req, res, next) => { res.headers['X-Custom'] = 'NestJS'; next(); }
Bexport function customHeader(req, res, next) { res.setHeader('X-Custom', 'NestJS'); next(); }
Cexport function customHeader(req: Request, res: Response, next: NextFunction) { res.setHeader('X-Custom', 'NestJS'); next(); }
Dexport function customHeader(req: Request, res: Response) { res.setHeader('X-Custom', 'NestJS'); }
Attempts:
2 left
💡 Hint
Check for correct TypeScript types and calling next() to continue.
lifecycle
advanced
2:00remaining
When is functional middleware executed in NestJS request lifecycle?
Choose the correct statement about when functional middleware runs during a request in NestJS.
ABefore any route handler or controller method is called
BAfter the controller method but before the response is sent
COnly after all guards and interceptors have run
DOnly after the response is sent to the client
Attempts:
2 left
💡 Hint
Middleware runs early to modify request or response before controllers.
🔧 Debug
advanced
2:00remaining
Why does this functional middleware cause the request to hang?
Examine this middleware and select the reason the request never completes.
NestJS
export function hangMiddleware(req, res, next) {
  console.log('Middleware started');
  // Missing next() call
}
ABecause res.send() is called twice causing an error
BBecause next() is never called, the request never moves forward
CBecause the middleware throws an exception before next()
DBecause the middleware modifies req.url incorrectly
Attempts:
2 left
💡 Hint
Middleware must call next() to continue the request chain.
state_output
expert
3:00remaining
What is the value of req.customData after this functional middleware runs?
Given this middleware that adds a customData property to the request, what will be the value of req.customData in the next middleware?
NestJS
import { Request, Response, NextFunction } from 'express';

interface CustomRequest extends Request {
  customData?: string;
}

export function addCustomData(req: CustomRequest, res: Response, next: NextFunction) {
  req.customData = 'NestJS Rocks!';
  next();
}
A'NestJS Rocks!'
Bnull
Cundefined
DThrows a TypeError because customData is not defined on Request
Attempts:
2 left
💡 Hint
The middleware adds customData property before calling next().