Discover how a few lines of middleware can save you hours of repetitive coding!
Why Functional middleware in NestJS? - Purpose & Use Cases
Imagine you have to check user permissions, log requests, and handle errors manually for every route in your NestJS app by repeating code everywhere.
Manually adding these checks in each route is tiring, easy to forget, and makes your code messy and hard to maintain.
Functional middleware lets you write small reusable functions that run before your route handlers, so you can add common logic cleanly and consistently.
app.get('/data', (req, res) => { if (!req.user) return res.status(401).send('No access'); log(req); res.send('data'); });
app.use(authMiddleware); app.use(logMiddleware); app.get('/data', (req, res) => { res.send('data'); });
It enables you to keep your routes simple and focus on business logic while middleware handles shared tasks automatically.
In a real app, you can add authentication middleware once, and it protects all routes without repeating code.
Manual checks in routes cause repetition and errors.
Functional middleware runs shared code before routes.
This keeps your app clean, consistent, and easier to maintain.
Practice
Solution
Step 1: Understand middleware role
Middleware runs before route handlers to process requests or add features like logging or authentication.Step 2: Identify correct purpose
Options A, B, and D relate to other parts of development, not middleware.Final Answer:
To run code before the route handler to modify requests or add features -> Option AQuick Check:
Middleware runs before routes = C [OK]
- Confusing middleware with database models
- Thinking middleware creates UI components
- Believing middleware compiles code
Solution
Step 1: Check middleware signature
Functional middleware must accept (req, res, next) and call next() to continue.Step 2: Validate options
function logger(req, res, next) { console.log(req.url); next(); } matches this pattern. class Logger { log(req, res) { console.log(req.url); } } is a class, not functional middleware. const logger = () => { console.log('log'); } lacks parameters and next call. function logger(req, res) { console.log(req.url); } misses next parameter and call.Final Answer:
function logger(req, res, next) { console.log(req.url); next(); } -> Option DQuick Check:
Middleware needs (req, res, next) and calls next() [OK]
- Omitting the next parameter
- Not calling next() inside middleware
- Using class instead of function for functional middleware
function logger(req, res, next) {
console.log('Request URL:', req.url);
next();
}Solution
Step 1: Understand req.url property
In middleware, req.url contains the path of the incoming request, here '/home'.Step 2: Analyze console.log output
The middleware logs 'Request URL:' followed by req.url, so it prints 'Request URL: /home'.Final Answer:
Request URL: /home -> Option BQuick Check:
req.url logs request path = A [OK]
- Assuming next() stops console.log
- Thinking req.url is undefined
- Confusing middleware with route handler output
function auth(req, res, next) {
if (!req.headers.authorization) {
res.status(401).send('Unauthorized');
}
next();
}Solution
Step 1: Analyze flow after sending response
If authorization header is missing, response is sent with 401, but next() is still called, which can cause errors by continuing the request cycle.Step 2: Identify correct fix
To fix, add return before res.status(401).send(...) to stop execution or call next() only if authorized.Final Answer:
next() is called even after sending response, causing errors -> Option CQuick Check:
Call next() only if no response sent [OK]
- Calling next() after res.send causes errors
- Forgetting to stop middleware after response
- Misusing res.status vs res.sendStatus
Solution
Step 1: Confirm middleware signature and header setting
Middleware must accept (req, res, next) and call next() after setting headers. res.setHeader is correct method.Step 2: Evaluate options
function addVersion(req, res, next) { res.setHeader('X-App-Version', '1.0'); next(); } sets header then calls next() correctly. function addVersion(req, res) { res.setHeader('X-App-Version', '1.0'); } misses next parameter and call. function addVersion(req, res, next) { next(); res.setHeader('X-App-Version', '1.0'); } calls next() before setting header, so header may not be sent. function addVersion(req, res, next) { req.setHeader('X-App-Version', '1.0'); next(); } uses req.setHeader which is incorrect because req cannot set response headers.Final Answer:
function addVersion(req, res, next) { res.setHeader('X-App-Version', '1.0'); next(); } -> Option AQuick Check:
Set header before next() with correct method [OK]
- Calling next() before setting headers
- Omitting next parameter or call
- Confusing req and res when setting headers
Ready to prove it?
Pass the test and this pattern moves unknown
