📖 Scenario: You are building a simple Express server that handles incoming requests. To help track each request uniquely, you want to add a request ID to every request. This ID will help you trace logs and debug issues easily.
🎯 Goal: Create an Express middleware that generates a unique request ID for each incoming request and attaches it to the request object. Then, use this ID in a route handler to send it back in the response.
📋 What You'll Learn
Create an Express app
Add middleware to generate a unique request ID using crypto.randomUUID()
Attach the request ID to req.id
Create a GET route at / that responds with the request ID in JSON format
💡 Why This Matters
🌍 Real World
Assigning unique request IDs helps developers trace and debug requests in logs, especially in complex or distributed systems.
💼 Career
Understanding middleware and request tracing is essential for backend developers working with Express or similar web frameworks.
Progress0 / 4 steps
1
Setup Express app
Create a variable called express by requiring the express module. Then create a variable called app by calling express().
Express
Hint
Use require('express') to import Express and call it as a function to create the app.
2
Add request ID middleware
Create a middleware function that uses crypto.randomUUID() to generate a unique ID. Attach this ID to req.id. Use app.use() to add this middleware to the app. Remember to require crypto at the top.
Express
Hint
Use app.use() to add a middleware that sets req.id to a new UUID.
3
Create GET route to return request ID
Add a GET route handler for path '/' using app.get(). In the handler, send a JSON response with a key requestId and value req.id.
Express
Hint
Use app.get() to create a route that sends back the request ID as JSON.
4
Start the server
Add code to start the Express server by calling app.listen() on port 3000. Provide a callback function that does nothing (empty arrow function).
Express
Hint
Use app.listen(3000, () => {}) to start the server on port 3000.
Practice
(1/5)
1. What is the main purpose of adding a request ID in an Express app?
easy
A. To encrypt the request data for security
B. To speed up the request processing time
C. To uniquely identify each request for easier tracking and debugging
D. To limit the number of requests from a user
Solution
Step 1: Understand the role of request ID
A request ID is used to uniquely mark each incoming request so it can be tracked through logs and debugging.
Step 2: Identify the main benefit
This helps developers trace requests easily, especially when many requests happen simultaneously.
Final Answer:
To uniquely identify each request for easier tracking and debugging -> Option C
Quick Check:
Request ID purpose = Unique tracking [OK]
Hint: Request ID helps track requests uniquely in logs [OK]
Common Mistakes:
Thinking request ID speeds up processing
Confusing request ID with encryption
Assuming request ID limits requests
2. Which code snippet correctly adds a unique request ID to each request using the 'uuid' library in Express middleware?