0
0
Expressframework~20 mins

res.json for JSON responses in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express JSON 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 route?
Consider this Express route handler:
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' }); });
AA 404 Not Found error
BA JSON response with Content-Type 'application/json' and body {"message":"Hello World"}
CAn HTML page displaying the text 'Hello World'
DA plain text response with body 'Hello World'
Attempts:
2 left
💡 Hint
res.json sends a JSON response with the correct content type.
📝 Syntax
intermediate
2: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?
Ares.status(201).json({ success: true });
Bres.sendStatus(201).json({ success: true });
Cres.json(201, { success: true });
Dres.json({ success: true }).status(201);
Attempts:
2 left
💡 Hint
Set status before sending JSON.
🔧 Debug
advanced
2:00remaining
What error does this code cause?
Examine this Express route:
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 }); });
ASends only the first JSON response 'Hello' and ignores the second
BSends a combined JSON response with both values
CThrows an error because res.json is called twice on the same response
DSends 'Hello' as JSON and then sends { done: true } as JSON successfully
Attempts:
2 left
💡 Hint
You cannot send multiple responses for one request.
🧠 Conceptual
advanced
2: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?
AIt converts the object to a JSON string and sets Content-Type to 'application/json'
BIt sends the object as is without conversion
CIt converts the object to XML format
DIt converts the object to a plain text string
Attempts:
2 left
💡 Hint
Think about how JSON responses are sent over HTTP.
state_output
expert
2:00remaining
What is the final response body and status code?
Given this Express route:
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); });
AStatus code 500 Internal Server Error
BStatus code 200 with JSON body {"ok":true}
CStatus code 202 with empty body
DStatus code 202 with JSON body {"ok":true}
Attempts:
2 left
💡 Hint
Calling res.status sets the status for the next response method.