Bird
Raised Fist0
Expressframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does method chaining on the Express res object allow you to do?
easy
A. Combine multiple response methods in one line for cleaner code
B. Run multiple server instances simultaneously
C. Chain database queries with response methods
D. Automatically restart the server on errors

Solution

  1. Step 1: Understand method chaining concept

    Method chaining means calling multiple methods one after another on the same object.
  2. Step 2: Apply to Express response object

    Express res allows chaining methods like status(), set(), and send() to write concise code.
  3. Final Answer:

    Combine multiple response methods in one line for cleaner code -> Option A
  4. Quick Check:

    Method chaining = Combine methods [OK]
Hint: Think of chaining like linking commands in one line [OK]
Common Mistakes:
  • Confusing method chaining with running multiple servers
  • Mixing database queries with response methods
  • Assuming chaining restarts the server
2. Which of the following is the correct way to chain methods on the Express res object?
easy
A. res.set('Content-Type', 'text/plain').status(200).send('OK')
B. res.status(200).send('OK').set('Content-Type', 'text/plain')
C. res.send('OK').status(200).set('Content-Type', 'text/plain')
D. res.status(200); res.send('OK'); res.set('Content-Type', 'text/plain')

Solution

  1. Step 1: Check method chaining order

    Methods like status() and set() return the res object, allowing chaining before send().
  2. Step 2: Identify correct chain ending

    The chain must end with send() to send the response and finish the chain.
  3. Final Answer:

    res.set('Content-Type', 'text/plain').status(200).send('OK') -> Option A
  4. Quick Check:

    Chain ends with send() [OK]
Hint: Always end chain with send() or end() [OK]
Common Mistakes:
  • Calling send() before setting status or headers
  • Trying to chain after send()
  • Using semicolons instead of chaining dots
3. What will be the output of this Express code snippet?
app.get('/', (req, res) => {
  res.status(404).set('Content-Type', 'text/plain').send('Not Found');
});
medium
A. Response with status 200 and body 'Not Found'
B. Syntax error due to incorrect chaining
C. Response with status 404 and empty body
D. Response with status 404 and body 'Not Found' with plain text content type

Solution

  1. Step 1: Analyze method chain

    The code sets status to 404, sets content type header, then sends 'Not Found' as body.
  2. Step 2: Understand response behavior

    The response will have status 404, header 'Content-Type: text/plain', and body 'Not Found'.
  3. Final Answer:

    Response with status 404 and body 'Not Found' with plain text content type -> Option D
  4. Quick Check:

    Status 404 + send body 'Not Found' [OK]
Hint: Status and headers set before send() affect response [OK]
Common Mistakes:
  • Assuming default status 200 is sent
  • Thinking body is empty
  • Believing chaining causes syntax error
4. Identify the error in this Express response chaining code:
res.send('Hello').status(200).set('Content-Type', 'text/html');
medium
A. No error, code works fine
B. Cannot chain methods after send() because response is already sent
C. Missing semicolon after send()
D. set() method does not exist on res object

Solution

  1. Step 1: Understand send() behavior

    Calling send() sends the response and ends it, so chaining after it is invalid.
  2. Step 2: Identify chaining error

    Methods status() and set() cannot be called after send() because response is finished.
  3. Final Answer:

    Cannot chain methods after send() because response is already sent -> Option B
  4. Quick Check:

    send() ends response, no chaining after [OK]
Hint: send() ends response; no chaining after it [OK]
Common Mistakes:
  • Thinking chaining after send() is allowed
  • Confusing semicolon placement with chaining error
  • Believing set() is not a valid method
5. You want to send a JSON response with status 201 and a custom header X-Custom set to Success. Which of the following chained methods is correct?
hard
A. res.status(201).send({ message: 'Created' }).set('X-Custom', 'Success');
B. res.status(201).json({ message: 'Created' }); res.set('X-Custom', 'Success');
C. res.set('X-Custom', 'Success').status(201).json({ message: 'Created' });
D. res.json({ message: 'Created' }).status(201).set('X-Custom', 'Success');

Solution

  1. Step 1: Order methods correctly

    Set headers first, then status, then send JSON with json() method.
  2. Step 2: Avoid chaining after response sent

    Methods after json() or send() won't work because response ends.
  3. Final Answer:

    res.set('X-Custom', 'Success').status(201).json({ message: 'Created' }); -> Option C
  4. Quick Check:

    Headers + status + json() in chain [OK]
Hint: Set headers and status before json() or send() [OK]
Common Mistakes:
  • Chaining set() after send() or json()
  • Sending response before setting headers
  • Using multiple separate statements instead of chaining
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard