It is a middleware that creates a shared space to store data related to a single HTTP request, allowing different parts of the app to access this data during that request.
Click to reveal answer
beginner
How does request context middleware help in handling data during a request?
It keeps data like user info or request ID accessible across multiple middleware and route handlers without passing it explicitly as parameters.
Click to reveal answer
beginner
Which Express object is commonly used to store request context data?
The req (request) object is often extended with custom properties to hold context data for the current request.
Click to reveal answer
intermediate
Why should request context data not be stored globally in Express apps?
Because Express handles many requests at once, global data would mix information between requests, causing bugs and security issues.
Click to reveal answer
beginner
Give an example of data you might store in request context middleware.
You might store a unique request ID, authenticated user details, or timing info to track how long the request takes.
Click to reveal answer
What is the main purpose of request context middleware in Express?
ATo store data globally for all requests
BTo share data across different parts of the same request
CTo handle static files
DTo manage database connections
✗ Incorrect
Request context middleware shares data only within the scope of a single request, not globally.
Where is request context data usually stored in Express?
AIn the <code>res</code> object
BIn a global variable
CIn the <code>req</code> object
DIn the <code>app</code> object
✗ Incorrect
The req object is specific to each request and is the right place to store context data.
Why is it bad to store request data in global variables?
AIt prevents bugs
BIt improves performance
CIt makes code easier to read
DIt causes data to mix between requests
✗ Incorrect
Global variables are shared across all requests, which can cause data leaks and bugs.
Which of these is NOT a typical use of request context middleware?
AServing static images
BTracking request duration
CStoring user authentication info
DPassing request ID
✗ Incorrect
Serving static images is handled by static middleware, not request context middleware.
How can you add data to the request context in Express middleware?
ABy adding properties to <code>req</code>, like <code>req.user = {...}</code>
BBy modifying the <code>res</code> headers
CBy changing global variables
DBy editing the <code>app</code> object
✗ Incorrect
Adding properties to req is the standard way to store request-specific data.
Explain what request context middleware is and why it is useful in Express apps.
Think about how to share data safely during one request.
You got /4 concepts.
Describe how you would implement simple request context middleware in Express.
Focus on modifying the req object inside middleware.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of request context middleware in Express?
easy
A. To serve static files like images and CSS
B. To handle database connections globally for all requests
C. To store and share data specific to each request across middleware and routes
D. To manage user authentication tokens only
Solution
Step 1: Understand request context middleware role
Request context middleware stores data that belongs to a single request, making it accessible throughout that request's lifecycle.
Step 2: Compare with other options
The other options describe different middleware roles but not the core purpose of request context middleware.
Final Answer:
To store and share data specific to each request across middleware and routes -> Option C
Quick Check:
Request context middleware = store per-request data [OK]
Hint: Remember: context middleware holds data per request only [OK]
Common Mistakes:
Confusing request context with global app state
Thinking it manages static files
Assuming it only handles authentication
2. Which of the following is the correct way to add a property to the request context in Express middleware?
easy
A. req.context = { userId: 123 }; next();
B. res.context.userId = 123; next();
C. app.context.userId = 123; next();
D. context.userId = 123; next();
Solution
Step 1: Identify where to store request-specific data
In Express, the request object (req) is unique per request, so adding a property to req is correct.
Step 2: Check other options for correctness
res is for response, app is global, and context alone is undefined, so only req.context works properly.
Final Answer:
req.context = { userId: 123 }; next(); -> Option A
Quick Check:
Store data on req object for request context [OK]
Hint: Attach context data to req, not res or app [OK]
Common Mistakes:
Using res or app instead of req for context
Forgetting to call next() after middleware
Assigning context to undefined variable
3. Given this middleware and route, what will be the output when a GET request is made to /user?
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.