Console logs before and after next() run synchronously; first logs "Middleware start".
Step 2: Understand next() behavior
Calling next() passes control but does not pause middleware; "Middleware end" logs after next() call.
Final Answer:
"Middleware start" then "Middleware end" -> Option A
Quick Check:
Logs before and after next() run in order [OK]
Hint: Logs before and after next() run in sequence [OK]
Common Mistakes:
Thinking next() pauses middleware execution
Assuming logs after next() run later
Believing next() stops middleware code
4. You wrote this NestJS middleware but requests never reach the handler. What is the problem?
export function authMiddleware(req, res, next) {
if (!req.headers.authorization) {
res.status(401).send('Unauthorized');
return;
}
next();
}
medium
A. Middleware must not send responses directly
B. Authorization header check is incorrect syntax
C. next() is called even after sending response, causing errors
D. next() should be called before checking headers
Solution
Step 1: Review middleware flow control
If authorization header is missing, response is sent but next() is still called, causing request to continue incorrectly.
Step 2: Fix flow by returning early
To stop processing after sending response, return immediately without calling next().
Final Answer:
next() is called even after sending response, causing errors -> Option C
Quick Check:
Don't call next() after sending response [OK]
Hint: Return after sending response to avoid calling next() [OK]
Common Mistakes:
Calling next() after response ends
Not returning early to stop middleware
Misunderstanding middleware flow control
5. You want to add a middleware that adds a timestamp to every request before handlers run. Which approach correctly ensures the timestamp is available in the handler?
hard
A. Add req.timestamp = Date.now() in middleware, then call next()
B. Call next() first, then add req.timestamp after
C. Add timestamp in the handler, not middleware
D. Send timestamp in response headers from middleware
Solution
Step 1: Understand middleware purpose
Middleware runs before handlers, so adding data to req here makes it available later.
Step 2: Correct order of operations
Assign req.timestamp before calling next() so handlers see the value.
Final Answer:
Add req.timestamp = Date.now() in middleware, then call next() -> Option A
Quick Check:
Add data before next() to share with handlers [OK]
Hint: Set request data before next() to share with handlers [OK]
Common Mistakes:
Adding data after next(), so handlers miss it
Trying to add data in handlers instead of middleware
Sending data only in response headers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.