0
0
Expressframework~20 mins

Response time tracking middleware in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Time Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1: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();
}
ALogs the time in milliseconds before the request starts processing
BLogs the time in milliseconds it took to handle the request after the response finishes
CLogs the time in seconds it took to handle the request immediately when middleware runs
DLogs the time in milliseconds it took to handle the request before calling next()
Attempts:
2 left
💡 Hint
Think about when the 'finish' event on the response object happens.
📝 Syntax
intermediate
1: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()
}
AChange the arrow function to a regular function to fix the syntax error
BReplace res.on with res.once to fix the syntax error
CAdd a semicolon after next() to fix the syntax error
DAdd a closing parenthesis after the res.on callback function before calling next()
Attempts:
2 left
💡 Hint
Check the parentheses balance around the res.on callback.
🔧 Debug
advanced
2: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`);
}
ABecause the duration is calculated immediately after next(), before response finishes
BBecause Date.now() returns time in seconds, not milliseconds
CBecause next() blocks the event loop until response finishes
DBecause the middleware does not listen to the 'close' event on res
Attempts:
2 left
💡 Hint
Think about when next() returns and when the response actually finishes.
🧠 Conceptual
advanced
1: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?
A'close' event
B'end' event
C'finish' event
D'data' event
Attempts:
2 left
💡 Hint
This event signals that all response data has been flushed.
state_output
expert
2: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.
AApproximately 150 (milliseconds)
BUndefined, because res.locals is not set synchronously
C0, because duration is calculated before response finishes
DNaN, because Date.now() returns a string
Attempts:
2 left
💡 Hint
res.locals can be set asynchronously inside event handlers.