0
0
Expressframework~10 mins

Performance monitoring basics in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Performance monitoring basics
Start Application
Collect Metrics
Analyze Metrics
Detect Performance Issues
Alert or Log
Optimize Application
Back to Collect Metrics
This flow shows how performance monitoring collects data, analyzes it, detects issues, alerts, and helps optimize the app continuously.
Execution Sample
Express
const express = require('express');
const app = express();
app.use((req, res, next) => {
  const start = Date.now();
  res.on('finish', () => {
    console.log(`Request took ${Date.now() - start} ms`);
  });
  next();
});
This code measures how long each HTTP request takes and logs the duration.
Execution Table
StepActionTimestamp Start (ms)Timestamp End (ms)Duration (ms)Output
1Request received1000
2Middleware start timer1000
3Request processing1000
4Response finish event triggered102525Request took 25 ms
5Log outputRequest took 25 ms
6Next middleware or route handler
💡 Request processing finished, duration logged, middleware passed control forward
Variable Tracker
VariableStartAfter Step 2After Step 4Final
startundefined100010001000
Durationundefinedundefined2525
Key Moments - 2 Insights
Why do we use 'res.on("finish")' instead of logging immediately after 'Date.now() - start'?
Because the response finishes asynchronously, logging inside 'res.on("finish")' ensures we measure the full request time, as shown in execution_table step 4.
What does 'next()' do in the middleware?
'next()' passes control to the next middleware or route handler, allowing the request to continue processing, as seen in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the duration of the request at step 4?
A25 ms
B1000 ms
C1025 ms
D0 ms
💡 Hint
Check the 'Duration (ms)' column at step 4 in the execution_table.
At which step does the middleware start the timer?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Look at the 'Action' column in execution_table for when the timer starts.
If we remove 'next()', what happens to the request flow?
ADuration logs twice
BRequest processing stops after middleware
CRequest continues normally
DResponse never finishes
💡 Hint
Refer to key_moments about the role of 'next()' and execution_table step 6.
Concept Snapshot
Performance monitoring basics in Express:
- Use middleware to measure request time
- Start timer at request start
- Use 'res.on("finish")' to detect response end
- Log or send metrics for analysis
- Use 'next()' to continue request flow
Full Transcript
Performance monitoring in Express involves adding middleware that records the time when a request starts and listens for the response to finish. When the response finishes, it calculates the total time taken and logs it. This helps detect slow requests. The middleware uses 'next()' to pass control to the next handler so the request can be processed fully. This cycle repeats for every request, enabling continuous monitoring and optimization.