QA
Intermediate LevelExpress JS

How do you measure and inject X-Response-Time HTTP headers using response-time middleware?

❓ Interview Prompt: Explain response-time middleware, measuring millisecond latency from request arrival to header emission, and injecting X-Response-Time headers.

The 'response-time' middleware automatically calculates the elapsed time (in milliseconds) from when a request enters the Express middleware stack to when response headers are written, appending an 'X-Response-Time: 12.4ms' header. Ideal for latency metrics telemetry.

Code Implementation Example

javascript
import express from 'express';
import responseTime from 'response-time';

const app = express();

// Automatically inject X-Response-Time header and record metrics
app.use(responseTime((req, res, time) => {
  console.log(`[${req.method}] ${req.url} completed in ${time.toFixed(2)}ms`);
}));
💡Calculating request latency and injecting X-Response-Time header.
🎯 Interview Key Takeaway

Use response-time middleware to measure request duration and inject X-Response-Time latency headers.

#response-time#Performance#Headers#Intermediate
Back to all Express JS Questions→

More Express JS Interview Questions

Beginner LevelExpress JS

What is Express.js and how does the middleware pipeline work?

Beginner LevelExpress JS

What is the difference between req.params, req.query, and req.body?

Beginner LevelExpress JS

How does express.Router() work for building modular route controllers?