B. Using Date.now() instead of process.hrtime() causes error
C. res.on('finish') should be res.on('end')
D. Missing call to next(), so request hangs
Solution
Step 1: Check middleware flow
Middleware must call next() to pass control; missing here causes request to hang.
Step 2: Validate event and logging
res.on('finish') is correct event; Date.now() is valid; logging is allowed.
Final Answer:
Missing call to next(), so request hangs -> Option D
Quick Check:
Always call next() in middleware [OK]
Hint: Always call next() to continue middleware chain [OK]
Common Mistakes:
Forgetting next() call
Confusing 'finish' with 'end' event
Thinking Date.now() causes errors
5. You want to log the response time in milliseconds to the console using the format Request took ${duration}ms. Which middleware code correctly does this?
To capture the full request processing time, record the start time, call next(), and log the duration in the 'finish' event handler which fires after the response is sent.
Step 2: Analyze incorrect approaches
Logging right after starting the timer yields nearly 0ms because handlers haven't executed yet. The 'close' event relates to connection closure, not response completion, and may not fire reliably for timing.
Final Answer:
Uses res.on('finish') to log accurate request duration -> Option A