Method chaining on response lets you write cleaner code by linking multiple response actions together. It makes your server replies easier to read and write.
Method chaining on response in Express
res.status(code).set(headerObject).send(body)You can chain many response methods because each returns the response object.
Common methods to chain: status(), set(), send(), json(), end().
res.status(200).send('OK')
res.status(404).send('Not Found')
res.status(201).set({'Content-Type': 'application/json'}).json({id: 1})
res.set('X-Custom-Header', 'value').status(200).send('Done')
This Express app has three routes. Each uses method chaining on the response object to set status, headers, and send data in one line.
import express from 'express'; const app = express(); app.get('/hello', (req, res) => { // Send a 200 status with a greeting message res.status(200).set({'Content-Type': 'text/plain'}).send('Hello, world!'); }); app.get('/json', (req, res) => { // Send JSON data with 200 status res.status(200).json({message: 'This is JSON data'}); }); app.get('/notfound', (req, res) => { // Send 404 status with message res.status(404).send('Page not found'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Method chaining improves readability and reduces extra variables.
Time complexity is negligible; it's just calling methods on the response object.
Common mistake: forgetting to end the chain with a method that sends or ends the response, like send() or end().
Method chaining lets you combine response methods for cleaner code.
Use it to set status, headers, and send data in one line.
Always end the chain with a method that sends or ends the response.
Practice
res object allow you to do?Solution
Step 1: Understand method chaining concept
Method chaining means calling multiple methods one after another on the same object.Step 2: Apply to Express response object
Expressresallows chaining methods likestatus(),set(), andsend()to write concise code.Final Answer:
Combine multiple response methods in one line for cleaner code -> Option AQuick Check:
Method chaining = Combine methods [OK]
- Confusing method chaining with running multiple servers
- Mixing database queries with response methods
- Assuming chaining restarts the server
res object?Solution
Step 1: Check method chaining order
Methods likestatus()andset()return theresobject, allowing chaining beforesend().Step 2: Identify correct chain ending
The chain must end withsend()to send the response and finish the chain.Final Answer:
res.set('Content-Type', 'text/plain').status(200).send('OK') -> Option AQuick Check:
Chain ends with send() [OK]
- Calling send() before setting status or headers
- Trying to chain after send()
- Using semicolons instead of chaining dots
app.get('/', (req, res) => {
res.status(404).set('Content-Type', 'text/plain').send('Not Found');
});Solution
Step 1: Analyze method chain
The code sets status to 404, sets content type header, then sends 'Not Found' as body.Step 2: Understand response behavior
The response will have status 404, header 'Content-Type: text/plain', and body 'Not Found'.Final Answer:
Response with status 404 and body 'Not Found' with plain text content type -> Option DQuick Check:
Status 404 + send body 'Not Found' [OK]
- Assuming default status 200 is sent
- Thinking body is empty
- Believing chaining causes syntax error
res.send('Hello').status(200).set('Content-Type', 'text/html');Solution
Step 1: Understand send() behavior
Callingsend()sends the response and ends it, so chaining after it is invalid.Step 2: Identify chaining error
Methodsstatus()andset()cannot be called aftersend()because response is finished.Final Answer:
Cannot chain methods after send() because response is already sent -> Option BQuick Check:
send() ends response, no chaining after [OK]
- Thinking chaining after send() is allowed
- Confusing semicolon placement with chaining error
- Believing set() is not a valid method
X-Custom set to Success. Which of the following chained methods is correct?Solution
Step 1: Order methods correctly
Set headers first, then status, then send JSON withjson()method.Step 2: Avoid chaining after response sent
Methods afterjson()orsend()won't work because response ends.Final Answer:
res.set('X-Custom', 'Success').status(201).json({ message: 'Created' }); -> Option CQuick Check:
Headers + status + json() in chain [OK]
- Chaining set() after send() or json()
- Sending response before setting headers
- Using multiple separate statements instead of chaining
Ready to prove it?
Pass the test and this pattern moves unknown
