A. Calling res.redirect after res.send causes an error
B. res.redirect should be called before res.send
C. res.send cannot send strings, only JSON
D. No error; this code works fine
Solution
Step 1: Understand response flow in Express
Once res.send() is called, the response is sent and closed.
Step 2: Check order of methods
Calling res.redirect() after res.send() tries to send headers again, causing an error.
Final Answer:
Calling res.redirect after res.send causes an error -> Option A
Quick Check:
Send ends response; redirect after send fails [OK]
Hint: Send or redirect ends response; don't call both [OK]
Common Mistakes:
Thinking order doesn't matter
Believing send only works with JSON
Assuming no error on multiple sends
5. You want to send a JSON response with status 400 and a message 'Invalid input'. Which code correctly does this in Express?
hard
A. res.json(400, { error: 'Invalid input' })
B. res.sendStatus(400).json({ error: 'Invalid input' })
C. res.status(400).send('Invalid input')
D. res.status(400).json({ error: 'Invalid input' })
Solution
Step 1: Set the status code correctly
Use res.status(400) to set the HTTP status to 400 (Bad Request).
Step 2: Send JSON response
Use json() to send the JSON object with the error message.
Step 3: Check incorrect options
res.json(400, { error: 'Invalid input' }) uses wrong method signature (sends 400 as JSON body with status 200); res.status(400).send('Invalid input') sends plain text string (Content-Type text/html) instead of JSON; res.sendStatus(400).json({ error: 'Invalid input' }) ends response with sendStatus before json.
Final Answer:
res.status(400).json({ error: 'Invalid input' }) -> Option D
Quick Check:
Status then json() sends JSON with code [OK]
Hint: Use status() then json() to send JSON with status [OK]
Common Mistakes:
Passing status code inside json()
Using send instead of json for JSON data
Calling sendStatus before json
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.