Challenge - 5 Problems
Functional Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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(); }
Attempts:
2 left
💡 Hint
Remember that next() passes control to the next middleware or route handler.
✗ Incorrect
The middleware logs the HTTP method and URL, then calls next() to continue processing. Since the request is GET to '/api/data', it logs 'GET /api/data'.
📝 Syntax
intermediate2: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.
Attempts:
2 left
💡 Hint
Check for correct TypeScript types and calling next() to continue.
✗ Incorrect
Option C correctly types the parameters and calls next() after setting the header. Option C misses types, C uses incorrect header syntax, D misses next parameter and call.
❓ lifecycle
advanced2:00remaining
When is functional middleware executed in NestJS request lifecycle?
Choose the correct statement about when functional middleware runs during a request in NestJS.
Attempts:
2 left
💡 Hint
Middleware runs early to modify request or response before controllers.
✗ Incorrect
Functional middleware runs before route handlers and controllers, allowing it to modify request or response early in the lifecycle.
🔧 Debug
advanced2: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
}Attempts:
2 left
💡 Hint
Middleware must call next() to continue the request chain.
✗ Incorrect
Without calling next(), the middleware blocks the request from continuing, causing it to hang.
❓ state_output
expert3: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(); }
Attempts:
2 left
💡 Hint
The middleware adds customData property before calling next().
✗ Incorrect
The middleware sets req.customData to 'NestJS Rocks!' so the next middleware sees this value.