The code uses res.status(201) which sets the HTTP status code to 201.
Step 2: Understand the meaning of 201
Status 201 means the request was successful and a new resource was created.
Final Answer:
201 Created -> Option A
Quick Check:
res.status(201) sends 201 Created [OK]
Hint: res.status(201) means resource created successfully [OK]
Common Mistakes:
Assuming default 200 status
Confusing 201 with 404
Ignoring the status() call
4. You wrote this Express code but clients always get status 200 instead of 400:
app.post('/submit', (req, res) => {
if (!req.body.name) {
res.status(400);
res.send('Name is required');
}
});
What is the main problem?
medium
A. res.send() must come before res.status()
B. res.status(400) must be followed by return to stop execution
C. 400 is not a valid status code
D. res.status() does not set the status code
Solution
Step 1: Analyze the code flow after setting status 400
res.status(400) sets the status but code continues to run after sending response.
Step 2: Understand Express response behavior
Without return, Express may continue and send default 200 later or cause errors.
Final Answer:
res.status(400) must be followed by return to stop execution -> Option B
Quick Check:
Use return after res.status().send() to stop further processing [OK]
Hint: Add return after res.status().send() to prevent default 200 [OK]
Common Mistakes:
Not returning after sending response
Calling res.send() before res.status()
Thinking 400 is invalid
5. You want to send a 204 No Content status after deleting a resource in Express. Which code snippet correctly does this?
hard
A. res.status(204).send()
B. res.sendStatus(204).send('Deleted')
C. res.status(204).send('Deleted')
D. res.send(204)
Solution
Step 1: Understand 204 No Content meaning
204 means success but no response body should be sent.
Step 2: Choose code that sends 204 without content
res.status(204).send() sends status 204 with empty body, which is correct.
Step 3: Identify incorrect options
res.sendStatus(204).send('Deleted') tries to chain send() after sendStatus(), which is invalid. res.status(204).send('Deleted') sends a body with 204, which breaks the rule. res.send(204) sends 204 as body, not status.
Final Answer:
res.status(204).send() -> Option A
Quick Check:
204 means no content, so send empty response [OK]
Hint: Use res.status(204).send() to send no content response [OK]