Recall & Review
beginner
What is method chaining on the response object in Express?
Method chaining on the response object means calling multiple response methods one after another in a single statement. This works because each method returns the response object itself, allowing smooth, readable code.
Click to reveal answer
beginner
Why does Express allow chaining methods like res.status().send()?
Express methods like res.status() return the response object itself. This lets you chain another method like res.send() right after, making the code shorter and easier to read.
Click to reveal answer
beginner
Example: What does this code do? <br>
res.status(404).send('Not Found')This sets the HTTP status code to 404 (Not Found) and then sends the text 'Not Found' as the response body, all in one chained statement.
Click to reveal answer
beginner
Can you chain res.json() after res.status() in Express?
Yes, you can chain res.json() after res.status() to set the status code and send a JSON response in one statement, like res.status(200).json({ message: 'OK' }).
Click to reveal answer
intermediate
What happens if you call res.send() twice in a chain?
Calling res.send() twice will cause an error because the response is already sent after the first call. Method chaining works only when the methods return the response object before sending the response.
Click to reveal answer
Which Express response method is commonly used to set the HTTP status code before sending a response?
✗ Incorrect
res.status() sets the HTTP status code and returns the response object for chaining.
What does this code do? <br>
res.status(201).json({ success: true })✗ Incorrect
It sets status 201 (Created) and sends a JSON object { success: true }.
Why is method chaining useful in Express response handling?
✗ Incorrect
Method chaining helps write cleaner and more readable code by combining calls.
Which of these is NOT a valid chained method on Express response?
✗ Incorrect
res.send() ends the response, so chaining res.status() after it is invalid.
What does res.send() do in Express?
✗ Incorrect
res.send() sends the response content and finishes the response.
Explain how method chaining works on the Express response object and why it is helpful.
Think about how you can write res.status(200).send('OK') instead of two separate lines.
You got /4 concepts.
Describe a common mistake when chaining methods on the Express response object and how to avoid it.
Remember that sending the response finishes it, so no more chaining after that.
You got /4 concepts.