0
0
Expressframework~10 mins

Request context middleware in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Request context middleware
Incoming HTTP Request
Middleware 1: Setup Context
Attach Context to req
Next Middleware or Route Handler
Use Context in Handlers
Send Response
Request Ends
The middleware intercepts each request, creates a context object, attaches it to the request, then passes control to the next handler which can use this context.
Execution Sample
Express
app.use((req, res, next) => {
  req.context = { requestId: Date.now() };
  next();
});

app.get('/', (req, res) => {
  res.send(`ID: ${req.context.requestId}`);
});
This middleware adds a unique requestId to each request's context, which the route handler then uses in the response.
Execution Table
StepActionreq.context BeforeAction Resultreq.context AfterNext Called
1Incoming request arrivesundefinedMiddleware runsundefinedNo
2Middleware sets req.contextundefinedreq.context = {requestId: timestamp}{requestId: 1680000000000}No
3Middleware calls next(){requestId: 1680000000000}Pass control to next handler{requestId: 1680000000000}Yes
4Route handler reads req.context{requestId: 1680000000000}Reads requestId{requestId: 1680000000000}N/A
5Route handler sends response{requestId: 1680000000000}Response sent with ID{requestId: 1680000000000}N/A
6Request ends{requestId: 1680000000000}Connection closed{requestId: 1680000000000}N/A
💡 Request ends after response is sent and connection closes.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
req.contextundefined{requestId: 1680000000000}{requestId: 1680000000000}{requestId: 1680000000000}
Key Moments - 3 Insights
Why is req.context undefined before the middleware runs?
Because req.context is not a built-in property; the middleware creates and attaches it at step 2 as shown in the execution_table.
What happens if next() is not called in the middleware?
The request will hang and never reach the route handler, because next() passes control forward, as seen between steps 2 and 3.
Can the route handler modify req.context?
Yes, since req.context is attached to the request object, the handler can read or update it after middleware sets it, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is req.context after step 2?
Anull
Bundefined
C{requestId: 1680000000000}
D{}
💡 Hint
Check the 'req.context After' column in row for step 2.
At which step does the middleware pass control to the next handler?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'Next Called' column showing 'Yes' in the execution_table.
If the middleware did not set req.context, what would the route handler see?
Areq.context would be undefined
Breq.context would be an empty object
Creq.context would be null
Dreq.context would have a default requestId
💡 Hint
Refer to the variable_tracker start value and step 2 in execution_table.
Concept Snapshot
Request context middleware in Express:
- Middleware runs on every request
- Creates a context object (e.g., req.context)
- Attaches it to req for downstream use
- Calls next() to continue processing
- Route handlers access req.context for request-specific data
Full Transcript
In Express, request context middleware runs on each incoming HTTP request. It creates a context object, such as a unique requestId, and attaches it to the request object as req.context. This allows later middleware or route handlers to access and use this context data. The middleware must call next() to pass control forward. The route handler then reads req.context and can use it to customize the response. The request ends after the response is sent and the connection closes.