Challenge - 5 Problems
Express JSON Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express route?
Consider this Express route handler:
What will the client receive when requesting '/data'?
app.get('/data', (req, res) => { res.json({ message: 'Hello World' }); });What will the client receive when requesting '/data'?
Express
app.get('/data', (req, res) => { res.json({ message: 'Hello World' }); });
Attempts:
2 left
💡 Hint
res.json sends a JSON response with the correct content type.
✗ Incorrect
The res.json method sends the JavaScript object as a JSON string with the Content-Type header set to 'application/json'.
📝 Syntax
intermediate2:00remaining
Which option correctly sends a JSON response with a status code 201?
You want to send a JSON response with status code 201 and body { success: true }. Which code is correct?
Attempts:
2 left
💡 Hint
Set status before sending JSON.
✗ Incorrect
The correct pattern is to call res.status(code) first, then res.json(body). Other options either use invalid method signatures or chain incorrectly.
🔧 Debug
advanced2:00remaining
What error does this code cause?
Examine this Express route:
What happens when a client requests '/test'?
app.get('/test', (req, res) => { res.json('Hello'); res.json({ done: true }); });What happens when a client requests '/test'?
Express
app.get('/test', (req, res) => { res.json('Hello'); res.json({ done: true }); });
Attempts:
2 left
💡 Hint
You cannot send multiple responses for one request.
✗ Incorrect
Calling res.json twice tries to send two responses, which is not allowed and causes an error.
🧠 Conceptual
advanced2:00remaining
How does res.json handle JavaScript objects?
When you pass a JavaScript object to res.json, what does Express do internally before sending the response?
Attempts:
2 left
💡 Hint
Think about how JSON responses are sent over HTTP.
✗ Incorrect
res.json converts the object to a JSON string and sets the correct content type header so the client knows it is JSON.
❓ state_output
expert2:00remaining
What is the final response body and status code?
Given this Express route:
What will the client receive?
app.get('/status', (req, res) => { const data = { ok: true }; res.status(202); res.json(data); });What will the client receive?
Express
app.get('/status', (req, res) => { const data = { ok: true }; res.status(202); res.json(data); });
Attempts:
2 left
💡 Hint
Calling res.status sets the status for the next response method.
✗ Incorrect
res.status(202) sets the status code, and res.json sends the JSON body with that status code.