0
0
Expressframework~30 mins

Performance monitoring basics in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Performance monitoring basics
📖 Scenario: You are building a simple Express.js server. You want to monitor how long each request takes to process. This helps you understand your server's performance and find slow parts.
🎯 Goal: Create an Express.js server that tracks the time taken to handle each request and logs it to the console.
📋 What You'll Learn
Create an Express.js app with a single GET route at '/'
Add a middleware to record the start time of each request
Calculate the duration after the response is sent
Log the request method, URL, and duration in milliseconds
💡 Why This Matters
🌍 Real World
Monitoring request durations helps developers find slow endpoints and improve user experience by making servers faster.
💼 Career
Performance monitoring is a key skill for backend developers and DevOps engineers to ensure applications run smoothly and efficiently.
Progress0 / 4 steps
1
Set up Express server
Create an Express app by requiring express and calling express(). Then create a GET route at '/' that sends the text 'Hello World' as the response.
Express
Need a hint?

Use require('express') to import Express. Then call express() to create the app. Use app.get('/', ...) to define the route.

2
Add start time middleware
Add a middleware using app.use that records the current time in milliseconds in req.startTime before passing control to the next middleware.
Express
Need a hint?

Use Date.now() to get the current time in milliseconds. Store it in req.startTime. Call next() to continue.

3
Calculate and log request duration
Add a listener for the 'finish' event on res inside the middleware. When the response finishes, calculate the duration by subtracting req.startTime from the current time. Then log the request method, URL, and duration in milliseconds using console.log.
Express
Need a hint?

Use res.on('finish', callback) to run code after response ends. Calculate duration with Date.now() - req.startTime. Use a template string to log method, URL, and duration.

4
Start server and test logging
Add app.listen to start the server on port 3000. Then add a console.log to print 'Server running on port 3000'. Run the server and visit http://localhost:3000/ to see the log of the request duration.
Express
Need a hint?

Use app.listen(3000, callback) to start the server. Inside the callback, print the message with console.log.