0
0
Expressframework~20 mins

Request ID for tracing in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request ID Tracing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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}`);
});
ALogs a unique UUID string for each request, e.g., 'Request ID: 123e4567-e89b-12d3-a456-426614174000'
BLogs 'Request ID: undefined' because req.id is not set
CThrows a ReferenceError because uuidv4 is not defined
DLogs the same fixed string 'Request ID: 1' for every request
Attempts:
2 left
💡 Hint
Look at how uuidv4() is called and assigned to req.id before logging.
📝 Syntax
intermediate
2: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?
Aapp.use((req, res) => { req.id = uuidv4(); next(); });
B;)} ;)(txen ;)(4vdiuu = di.qer { >= )txen ,ser ,qer((esu.ppa
Capp.use((req, res, next) => { req.id = uuidv4(); return next; });
Dapp.use((req, res, next) => { req.id = uuidv4(); next(); });
Attempts:
2 left
💡 Hint
Middleware functions must have three parameters and call next() as a function.
🔧 Debug
advanced
2: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();
});
Auuidv4() is not imported correctly, causing a runtime error
BThe UUID is generated but never assigned to req.id, so req.id is undefined
CMiddleware order is wrong; logging happens before UUID generation
Dnext() is not called in the first middleware, blocking the chain
Attempts:
2 left
💡 Hint
Check if the generated UUID is saved anywhere on the request object.
state_output
advanced
2: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);
});
AA unique UUID string generated by uuidv4()
BThe string 'fixed-id' from the first middleware
Cundefined because req.id is overwritten with undefined
DAn error because req.id is assigned twice
Attempts:
2 left
💡 Hint
Later middleware can overwrite properties set by earlier middleware.
🧠 Conceptual
expert
2: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?
AIt automatically secures the app by encrypting request data with the ID
BIt improves the speed of the server by caching requests with the same ID
CIt helps track and correlate logs and errors for individual requests across asynchronous operations
DIt replaces the need for session management by identifying users
Attempts:
2 left
💡 Hint
Think about how logs from different parts of the app can be connected.