Request context middleware helps keep data related to a single request in one place. It makes sharing information between parts of your app easier without passing many variables around.
You want to store user info after login and access it in different routes.
You need to track request-specific data like request ID for logging.
You want to share database connections or settings during a request.
You want to avoid passing many parameters through multiple functions.
You want to add timing or performance info for each request.
Syntax
Express
function requestContextMiddleware(req, res, next) {
req.context = { /* your data here */ };
next();
}
Middleware is a function that runs during the request before your route handlers.
You add a new property (like req.context) to store data for that request.
Examples
This example adds a unique requestId to each request using the current time.
B. req.context is undefined, so assigning user property causes an error
C. res object is used incorrectly
D. next() is missing, so request hangs
Solution
Step 1: Check initialization of req.context
The code tries to assign user to req.context.user, but req.context was never created, so it's undefined.
Step 2: Understand the error cause
Accessing a property on undefined causes a runtime error. next() is called, so no hanging issue.
Final Answer:
req.context is undefined, so assigning user property causes an error -> Option B
Quick Check:
Initialize req.context before adding properties [OK]
Hint: Always initialize req.context before use [OK]
Common Mistakes:
Not initializing req.context as an object
Forgetting to call next()
Misusing res instead of req
5. You want to store a request ID in the context for every request and access it in multiple routes. Which is the best way to implement this in Express?
hard
A. Pass requestId as a query parameter in every route URL
B. Store requestId in a global variable and access it in routes
C. Attach requestId to res.locals and access it in routes
D. Create middleware that sets req.context = { requestId: uniqueId } and call next(), then read req.context.requestId in routes
Solution
Step 1: Understand request-specific data storage
Request ID is unique per request, so it must be stored in a per-request object like req.context.
Step 2: Evaluate options for sharing data
Global variables cause conflicts between concurrent requests, query parameters are not suitable for internal data sharing, and res.locals is primarily for template data rather than request processing.
Final Answer:
Create middleware that sets req.context = { requestId: uniqueId } and call next(), then read req.context.requestId in routes -> Option D
Quick Check:
Use req.context for per-request data sharing [OK]
Hint: Use middleware to set req.context with request ID [OK]
Common Mistakes:
Using global variables for request data
Relying on query parameters for context
Confusing res.locals with request context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.