This program shows two middleware functions running in order before the route handler. The first logs the request, the second adds a header, then the route sends a response.
A. Middleware 1 does not call next(), so Middleware 2 runs twice
B. Middleware 2 will never run because next() is missing
C. Calling next() after res.send() causes an error or unexpected behavior
D. There is no problem; this order is correct
Solution
Step 1: Understand res.send() effect
Calling res.send() sends the response and ends the request cycle.
Step 2: Analyze calling next() after res.send()
Calling next() after res.send() can cause errors or unexpected behavior because the response is already sent.
Final Answer:
Calling next() after res.send() causes an error or unexpected behavior -> Option C
Quick Check:
Don't call next() after res.send() = A [OK]
Hint: Never call next() after sending response [OK]
Common Mistakes:
Thinking next() is optional after res.send()
Assuming middleware 2 runs normally
Ignoring that response ends request
5. You want to add a logging middleware that logs every request, an authentication middleware that blocks unauthorized users, and a route handler. Which order ensures unauthorized requests are blocked before routes run, but all requests are logged?
hard
A. Authentication middleware, Logging middleware, Route handler
B. Logging middleware, Authentication middleware, Route handler
C. Route handler, Logging middleware, Authentication middleware
D. Route handler, Authentication middleware, Logging middleware
Solution
Step 1: Determine middleware purpose order
Logging should happen for all requests first, so it must be first.
Step 2: Place authentication before route handler
Authentication must run before routes to block unauthorized access.
Step 3: Confirm route handler last
Route handler runs last after middleware passes.
Final Answer:
Logging middleware, Authentication middleware, Route handler -> Option B
Quick Check:
Log first, then auth, then route = A [OK]
Hint: Log first, authenticate second, handle route last [OK]
Common Mistakes:
Putting authentication before logging misses some logs
Running route before middleware breaks flow
Assuming order does not affect blocking unauthorized users
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.