Performance monitoring helps you see how well your app runs. It shows if your app is slow or uses too much memory.
Performance monitoring basics in Express
Introduction
Syntax
Express
const express = require('express'); const app = express(); // Use a middleware to measure response time app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { const duration = Date.now() - start; console.log(`${req.method} ${req.url} took ${duration}ms`); }); next(); });
This example uses middleware to measure how long each request takes.
It logs the method, URL, and time in milliseconds after the response finishes.
Examples
Express
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
const duration = Date.now() - start;
console.log(`${req.method} ${req.url} took ${duration}ms`);
});
next();
});Express
app.get('/', (req, res) => { res.send('Hello World!'); });
Sample Program
This program starts a server on port 3000. It logs how long each request takes. When you visit the homepage, it responds with 'Hello World!'.
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { const duration = Date.now() - start; console.log(`${req.method} ${req.url} took ${duration}ms`); }); next(); }); app.get('/', (req, res) => { res.send('Hello World!'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
Performance times may vary depending on your computer speed.
Use this simple method to find slow routes or middleware.
For more detailed monitoring, consider tools like New Relic or Prometheus.
Summary
Performance monitoring shows how fast your app responds.
Middleware can measure request time easily in Express.
Logging response times helps find slow parts to improve.
Practice
1. What is the main purpose of performance monitoring in an Express app?
easy
Solution
Step 1: Understand performance monitoring
Performance monitoring tracks how quickly the app handles requests.Step 2: Identify the main goal
The goal is to find slow parts and improve speed, not to add features or change UI.Final Answer:
To measure how fast the app responds to requests -> Option AQuick Check:
Performance monitoring = measure response speed [OK]
Hint: Performance monitoring = measuring app speed [OK]
Common Mistakes:
- Confusing performance monitoring with adding features
- Thinking it changes the user interface
- Assuming it stores user data
2. Which Express middleware code snippet correctly measures request duration?
easy
Solution
Step 1: Check middleware timing logic
app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { console.log(Date.now() - start); }); next(); }); starts timer, waits for response finish event, then logs duration correctly.Step 2: Compare other options
app.use((req, res, next) => { const start = Date.now(); next(); console.log(Date.now() - start); }); logs too early before response ends. Another option lacks next() call. The remaining option logs 'Start' and 'End' immediately, not duration.Final Answer:
app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { console.log(Date.now() - start); }); next(); }); -> Option AQuick Check:
Use 'res.on("finish")' to measure full request time [OK]
Hint: Use 'res.on("finish")' event to log after response ends [OK]
Common Mistakes:
- Logging time before response finishes
- Forgetting to call next() in middleware
- Not using response events to measure duration
3. Given this middleware, what will be logged when a request takes 150ms?
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
console.log(`Response time: ${Date.now() - start}ms`);
});
next();
});medium
Solution
Step 1: Understand timer start and finish
The timer starts at request start and logs when response finishes, so it measures full duration.Step 2: Calculate logged time
If request takes 150ms, logged time is approximately 150ms.Final Answer:
Response time: 150ms -> Option DQuick Check:
Timer logs actual elapsed time on 'finish' event [OK]
Hint: Logs time difference on response finish event [OK]
Common Mistakes:
- Assuming log happens immediately (0ms)
- Confusing timer start and end
- Expecting no output without console.log
4. Why does this middleware fail to log correct response time?
app.use((req, res, next) => {
const start = Date.now();
console.log(Date.now() - start);
next();
});medium
Solution
Step 1: Analyze when logging happens
Logging happens immediately after start, before response finishes, so duration is near zero.Step 2: Check middleware flow
next() is called, so request continues; no wrong event used; logging is too early.Final Answer:
It logs before response finishes, so time is near zero -> Option BQuick Check:
Log after response finish for correct timing [OK]
Hint: Log after response finishes, not before [OK]
Common Mistakes:
- Logging too early before response ends
- Forgetting to call next() (not here though)
- Using wrong event to measure time
5. You want to log slow requests taking over 500ms in Express. Which code correctly implements this?
hard
Solution
Step 1: Understand timing and event usage
app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { if (Date.now() - start > 500) console.log('Slow request'); }); next(); }); measures time from request start to response finish, then checks if duration > 500ms.Step 2: Check other options
Other options either check time immediately (always near zero), use wrong conditions like Date.now() > 500, or log unconditionally for all requests.Final Answer:
app.use((req, res, next) => { const start = Date.now(); res.on('finish', () => { if (Date.now() - start > 500) console.log('Slow request'); }); next(); }); -> Option CQuick Check:
Check duration on 'finish' event, then log if slow [OK]
Hint: Check time after response finishes, then log if slow [OK]
Common Mistakes:
- Checking time too early (before response ends)
- Logging all requests as slow
- Not using 'finish' event to measure full duration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
