Bird
Raised Fist0
Expressframework~20 mins

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

Choose your learning style10 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.

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

  1. 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.
  2. Step 2: Identify the main benefit

    This helps developers trace requests easily, especially when many requests happen simultaneously.
  3. Final Answer:

    To uniquely identify each request for easier tracking and debugging -> Option C
  4. 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?
easy
A. app.use((req, res, next) => { req.id = uuid.v4(); next(); });
B. app.use((req, res) => { req.id = uuid.v4(); });
C. app.use((req, res, next) => { res.id = uuid.v4(); next(); });
D. app.use((req, res, next) => { req.id = uuid(); next(); });

Solution

  1. Step 1: Check middleware signature

    Express middleware must have three parameters: req, res, next to pass control.
  2. Step 2: Verify correct uuid usage

    uuid.v4() generates a unique ID; assigning it to req.id is correct.
  3. Final Answer:

    app.use((req, res, next) => { req.id = uuid.v4(); next(); }); -> Option A
  4. Quick Check:

    Correct middleware and uuid call = app.use((req, res, next) => { req.id = uuid.v4(); next(); }); [OK]
Hint: Middleware needs (req, res, next) and uuid.v4() for ID [OK]
Common Mistakes:
  • Omitting next() call in middleware
  • Assigning ID to res instead of req
  • Calling uuid() instead of uuid.v4()
3. Given this middleware code, what will be logged when a request is made?
const { v4: uuidv4 } = require('uuid');
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}`);
});
medium
A. Logs 'undefined' and responds with 'Your request ID is undefined'
B. Logs a unique UUID string and responds with the same ID
C. Throws an error because req.id is not set
D. Logs a fixed string 'Request ID: 1234' every time

Solution

  1. Step 1: Analyze middleware behavior

    The middleware assigns a new UUID to req.id and logs it before calling next().
  2. Step 2: Check route handler response

    The route sends back the same req.id value, so the response matches the logged ID.
  3. Final Answer:

    Logs a unique UUID string and responds with the same ID -> Option B
  4. Quick Check:

    UUID assigned and logged = Logs a unique UUID string and responds with the same ID [OK]
Hint: Middleware sets req.id before route uses it [OK]
Common Mistakes:
  • Assuming req.id is undefined in route
  • Thinking the ID is fixed or static
  • Expecting an error due to missing ID
4. Identify the error in this Express middleware that tries to add a request ID:
app.use((req, res, next) => {
  req.id = uuidv4;
  next();
});
medium
A. req.id should be assigned to res.id
B. Middleware is missing the next parameter
C. next() is called before assigning req.id
D. uuidv4 is assigned without calling it as a function

Solution

  1. Step 1: Check uuidv4 usage

    uuidv4 is a function and must be called with parentheses to generate an ID.
  2. Step 2: Confirm middleware parameters and flow

    Middleware has correct parameters and calls next() after assignment, so no issue there.
  3. Final Answer:

    uuidv4 is assigned without calling it as a function -> Option D
  4. Quick Check:

    Function call missing parentheses = uuidv4 is assigned without calling it as a function [OK]
Hint: Call uuidv4() with () to get ID, not just uuidv4 [OK]
Common Mistakes:
  • Forgetting parentheses on function calls
  • Mixing req and res objects
  • Calling next() too early
5. You want to add a request ID to each request and include it in all logs and the response header. Which approach correctly achieves this in Express?
hard
A. Use middleware to assign req.id = uuidv4(), add res.set('X-Request-ID', req.id), then log req.id in all handlers
B. Assign req.id in each route handler separately without middleware
C. Generate a random number in middleware but do not pass it to response headers
D. Use middleware to assign req.id but never call next() to stop processing

Solution

  1. Step 1: Assign request ID in middleware

    Middleware is the right place to assign req.id once per request using uuidv4().
  2. Step 2: Add request ID to response headers and logs

    Setting res.set('X-Request-ID', req.id) sends the ID to clients; logging req.id in handlers tracks requests.
  3. Final Answer:

    Use middleware to assign req.id = uuidv4(), add res.set('X-Request-ID', req.id), then log req.id in all handlers -> Option A
  4. Quick Check:

    Middleware + header + logging = Use middleware to assign req.id = uuidv4(), add res.set('X-Request-ID', req.id), then log req.id in all handlers [OK]
Hint: Assign ID once in middleware, add header, log everywhere [OK]
Common Mistakes:
  • Assigning ID in every route instead of middleware
  • Not adding request ID to response headers
  • Forgetting to call next() in middleware
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard