0
0
Expressframework~20 mins

Method chaining on response in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express response chaining?
Consider this Express route handler code. What will the client receive as the response body?
Express
app.get('/test', (req, res) => {
  res.status(201).type('text/plain').send('Success');
});
AThe client receives a plain text response with status 201 and body 'Success'.
BThe client receives a JSON response with status 200 and body 'Success'.
CThe client receives a plain text response with status 200 and body 'Success'.
DThe client receives a JSON response with status 201 and body 'Success'.
Attempts:
2 left
💡 Hint
Remember that method chaining sets status, content type, and then sends the body.
📝 Syntax
intermediate
2:00remaining
Which option correctly chains methods to send JSON with status 404?
Choose the correct Express response chaining to send a JSON error message with status 404.
Ares.status(404).sendJson({ error: 'Not found' });
Bres.json({ error: 'Not found' }).status(404);
Cres.send({ error: 'Not found' }).status(404);
Dres.status(404).json({ error: 'Not found' });
Attempts:
2 left
💡 Hint
The status must be set before sending the response.
🔧 Debug
advanced
2:00remaining
Why does this chained response code cause an error?
Examine the code below. Why will it cause an error or unexpected behavior?
Express
app.get('/error', (req, res) => {
  res.send('First response').status(200);
});
ABecause status(200) is invalid and should be statusCode(200).
BBecause send cannot be called without a JSON object.
CBecause status must be set before send; chaining status after send does nothing or errors.
DBecause res.send returns undefined and chaining is not allowed.
Attempts:
2 left
💡 Hint
Think about the order of method calls and when headers are sent.
state_output
advanced
2:00remaining
What is the final status code sent to the client?
Given this Express code, what status code will the client receive?
Express
app.get('/chain', (req, res) => {
  res.status(202).status(204).send('Done');
});
A202
B204
C200
DError thrown due to multiple status calls
Attempts:
2 left
💡 Hint
Later status calls overwrite earlier ones.
🧠 Conceptual
expert
2:00remaining
Which statement about Express response method chaining is TRUE?
Select the true statement about method chaining on Express response objects.
ACalling res.send() ends the response and further chaining methods have no effect.
BYou can chain res.send() multiple times to send multiple responses.
Cres.json() cannot be chained with res.status() because they return different objects.
Dres.status() must always be called after res.send() to set the status code correctly.
Attempts:
2 left
💡 Hint
Think about when the response is finalized.