Challenge - 5 Problems
Request ID Tracing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express middleware adding a request ID?
Consider this Express middleware that adds a unique request ID to each incoming request and logs it. What will be logged when a request is handled?
Express
import express from 'express'; import { v4 as uuidv4 } from 'uuid'; const app = express(); app.use((req, res, next) => { req.id = uuidv4(); console.log(`Request ID: ${req.id}`); next(); }); app.get('/', (req, res) => { res.send(`Your request ID is ${req.id}`); });
Attempts:
2 left
💡 Hint
Look at how uuidv4() is called and assigned to req.id before logging.
✗ Incorrect
The middleware assigns a new UUID to req.id for each request, then logs it. This ensures each request has a unique ID for tracing.
📝 Syntax
intermediate2:00remaining
Which option correctly adds a request ID middleware in Express?
You want to add a middleware that attaches a unique request ID to each request object. Which code snippet is syntactically correct and works as intended?
Attempts:
2 left
💡 Hint
Middleware functions must have three parameters and call next() as a function.
✗ Incorrect
Option D correctly defines middleware with (req, res, next) and calls next() properly. Option D misses next parameter, C misses semicolon causing syntax error, D returns next function instead of calling it.
🔧 Debug
advanced2:00remaining
Why does this Express app fail to log request IDs correctly?
This Express app tries to log request IDs but logs 'undefined' instead. What is the cause?
Express
import express from 'express'; import { v4 as uuidv4 } from 'uuid'; const app = express(); app.use((req, res, next) => { uuidv4(); next(); }); app.use((req, res, next) => { console.log(`Request ID: ${req.id}`); next(); });
Attempts:
2 left
💡 Hint
Check if the generated UUID is saved anywhere on the request object.
✗ Incorrect
The first middleware calls uuidv4() but does not assign its result to req.id. So req.id remains undefined when logged.
❓ state_output
advanced2:00remaining
What is the value of req.id after this middleware chain?
Given these two middlewares, what will be the final value of req.id when the request reaches the route handler?
Express
import express from 'express'; import { v4 as uuidv4 } from 'uuid'; const app = express(); app.use((req, res, next) => { req.id = 'fixed-id'; next(); }); app.use((req, res, next) => { req.id = uuidv4(); next(); }); app.get('/', (req, res) => { res.send(req.id); });
Attempts:
2 left
💡 Hint
Later middleware can overwrite properties set by earlier middleware.
✗ Incorrect
The second middleware overwrites req.id with a new UUID, so the final value is the UUID string.
🧠 Conceptual
expert2:00remaining
Why is adding a request ID important for tracing in Express apps?
Which of the following best explains why adding a unique request ID to each request is useful in Express applications?
Attempts:
2 left
💡 Hint
Think about how logs from different parts of the app can be connected.
✗ Incorrect
A unique request ID allows developers to follow the flow of a single request through logs and error reports, especially when asynchronous code is involved.