0
0
Expressframework~10 mins

Method chaining on response in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method chaining on response
Start with res object
Call first method on res
Method returns res
Call next method on returned res
Repeat chaining methods
Send response to client
End
The response object methods return the response itself, allowing multiple methods to be called one after another in a chain, finishing with sending the response.
Execution Sample
Express
res.status(200).type('json').send({ message: 'OK' });
Sets status to 200, sets content type to JSON, then sends a JSON response.
Execution Table
StepMethod Calledres State ChangeReturn ValueEffect
1res.status(200)statusCode set to 200resStatus code set to 200
2res.type('json')Content-Type set to application/jsonresContent-Type header set
3res.send({ message: 'OK' })Response body setresResponse sent to client
💡 Response sent, method chain ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
res.statusCodeundefined200200200
res.headers['Content-Type']undefinedapplication/jsonapplication/jsonapplication/json
res.bodyundefinedundefinedundefined{"message":"OK"}
Key Moments - 2 Insights
Why can we call multiple methods on res one after another?
Because each method returns the res object itself, allowing the next method to be called on it, as shown in execution_table steps 1 and 2.
What happens if we call send() before setting status or type?
The response is sent immediately with whatever state res has at that moment, so status or headers set after send() won't affect the response, as send() ends the chain (step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Content-Type header after step 2?
Atext/html
Bundefined
Capplication/json
Dapplication/xml
💡 Hint
Check the 'res.headers["Content-Type"]' value in variable_tracker after step 2
At which step is the response actually sent to the client?
AStep 1
BStep 3
CStep 2
DAfter all steps
💡 Hint
Look at the 'Effect' column in execution_table for when response is sent
If res.send() returned something other than res, how would method chaining be affected?
AChaining would break after send()
BChaining would still work normally
CIt would cause an error immediately
DIt would send multiple responses
💡 Hint
Method chaining depends on each method returning res, see execution_table return values
Concept Snapshot
res.method() calls return res itself
Allows chaining like res.status().type().send()
Each method sets part of response
send() ends chain by sending response
Chaining improves code readability
Full Transcript
In Express, the response object (res) supports method chaining because each method returns the res object itself. This lets you call multiple methods in a row, like setting status, content type, and then sending the response. For example, res.status(200).type('json').send({ message: 'OK' }) sets the status code to 200, sets the Content-Type header to application/json, and sends a JSON response. The execution table shows each step: first setting status, then type, then sending. The variable tracker shows how res.statusCode, headers, and body change after each step. Key points are that chaining works because methods return res, and send() ends the chain by sending the response. If send() did not return res, chaining would break. This pattern makes Express code clean and easy to read.