0
0
Expressframework~10 mins

Response time tracking middleware in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a middleware function that records the start time.

Express
function responseTimeTracker(req, res, next) {
  req.startTime = [1];
  next();
}
Drag options to blanks, or click blank then click option'
Aperformance.now()
Bnew Date().getTime()
CDate.now()
Dprocess.hrtime()
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.hrtime() returns an array, not a timestamp.
Using performance.now() is not available in all Node.js versions.
2fill in blank
medium

Complete the code to calculate the response time in milliseconds after the response finishes.

Express
function responseTimeTracker(req, res, next) {
  req.startTime = Date.now();
  res.on('finish', () => {
    const duration = [1] - req.startTime;
    console.log(`Response time: ${duration}ms`);
  });
  next();
}
Drag options to blanks, or click blank then click option'
Anew Date()
BDate.now()
Cperformance.now()
Dprocess.hrtime()
Attempts:
3 left
💡 Hint
Common Mistakes
Using new Date() returns a Date object, not a number.
Using process.hrtime() returns an array, not a number.
3fill in blank
hard

Fix the error in the middleware to correctly log the response time in seconds with two decimals.

Express
function responseTimeTracker(req, res, next) {
  req.startTime = Date.now();
  res.on('finish', () => {
    const duration = (Date.now() - req.startTime) / [1];
    console.log(`Response time: ${duration.toFixed(2)}s`);
  });
  next();
}
Drag options to blanks, or click blank then click option'
A60
B10000
C100
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by 60 converts to minutes, not seconds.
Dividing by 100 or 10000 gives incorrect time units.
4fill in blank
hard

Fill both blanks to create a middleware that adds a custom header with the response time in milliseconds.

Express
function responseTimeTracker(req, res, next) {
  req.startTime = Date.now();
  res.on('[1]', () => {
    const duration = Date.now() - req.startTime;
    res.setHeader('[2]', duration + 'ms');
  });
  next();
}
Drag options to blanks, or click blank then click option'
Afinish
Bclose
Cresponse-time
DX-Response-Time
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' event may not always indicate response finished.
Using 'response-time' as header name is uncommon and may not be recognized.
5fill in blank
hard

Fill all three blanks to create a complete Express app using the response time middleware and a simple route.

Express
const express = require('express');
const app = express();

function responseTimeTracker(req, res, next) {
  req.startTime = Date.now();
  res.on('[1]', () => {
    const duration = Date.now() - req.startTime;
    res.setHeader('[2]', duration + 'ms');
  });
  next();
}

app.use([3]);

app.get('/', (req, res) => {
  res.send('Hello World');
});

app.listen(3000);
Drag options to blanks, or click blank then click option'
Afinish
BX-Response-Time
CresponseTimeTracker
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' event instead of 'finish'.
Forgetting to pass the middleware function to app.use.