Challenge - 5 Problems
Response Time Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does this Express middleware log?
Consider this Express middleware that tracks response time. What will it log to the console when a request is made?
Express
function responseTimeLogger(req, res, next) {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`Request took ${duration}ms`);
});
next();
}Attempts:
2 left
💡 Hint
Think about when the 'finish' event on the response object happens.
✗ Incorrect
The middleware records the start time, then waits for the 'finish' event on the response, which means the response has been sent. It then calculates the difference to find how long the request took.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in this response time middleware
Which option correctly fixes the syntax error in this Express middleware code?
Express
function responseTime(req, res, next) {
const start = Date.now()
res.on('finish', () => {
const duration = Date.now() - start
console.log('Response time: ' + duration + 'ms')
});
next()
}Attempts:
2 left
💡 Hint
Check the parentheses balance around the res.on callback.
✗ Incorrect
The res.on callback function is missing a closing parenthesis before next() is called, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this middleware log zero for response time?
This middleware always logs 'Request took 0ms' even for slow requests. Why?
Express
function responseTime(req, res, next) {
const start = Date.now();
next();
const duration = Date.now() - start;
console.log(`Request took ${duration}ms`);
}Attempts:
2 left
💡 Hint
Think about when next() returns and when the response actually finishes.
✗ Incorrect
Calling next() passes control to the next middleware but does not wait for the response to finish. The duration is measured immediately after next(), so it is near zero.
🧠 Conceptual
advanced1:30remaining
What is the best event to track response completion in Express middleware?
Which event on the response object should you listen to in order to accurately measure when the response has fully finished sending?
Attempts:
2 left
💡 Hint
This event signals that all response data has been flushed.
✗ Incorrect
The 'finish' event is emitted when the response has been sent completely. 'end' and 'data' are for readable streams, and 'close' can happen on errors or aborts.
❓ state_output
expert2:30remaining
What is the value of res.locals.duration after this middleware runs?
Given this middleware that tracks response time and stores it in res.locals, what will be the value of res.locals.duration after the response finishes?
Express
function responseTimeMiddleware(req, res, next) {
const start = Date.now();
res.on('finish', () => {
res.locals.duration = Date.now() - start;
});
next();
}
// Assume this middleware is used in an Express app and a request takes 150ms to complete.Attempts:
2 left
💡 Hint
res.locals can be set asynchronously inside event handlers.
✗ Incorrect
The duration is calculated inside the 'finish' event handler, which runs after the response completes. So res.locals.duration will hold the actual time in milliseconds, about 150.