0
0
Expressframework~10 mins

res.json for JSON responses in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to send a JSON response with Express.

Express
app.get('/data', (req, res) => {
  res.[1]({ message: 'Hello World' });
});
Drag options to blanks, or click blank then click option'
Ajson
Bwrite
Crender
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send() which can send JSON but does not explicitly set the content type.
Using res.render() which is for templates, not JSON.
Using res.write() which is a lower-level method.
2fill in blank
medium

Complete the code to send a JSON object with a status code 200.

Express
app.get('/status', (req, res) => {
  res.status(200).[1]({ success: true });
});
Drag options to blanks, or click blank then click option'
Asend
Bwrite
Cend
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send() which may not set the content type explicitly.
Using res.end() which ends the response without sending JSON.
Using res.write() which is not typical for JSON responses.
3fill in blank
hard

Fix the error in sending a JSON response with an object.

Express
app.get('/user', (req, res) => {
  res.json({ name: 'Alice', age: [1] });
});
Drag options to blanks, or click blank then click option'
A'25'
B"25"
C25
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Putting numbers inside quotes, making them strings.
Using a variable name instead of a value.
Using single or double quotes around numbers.
4fill in blank
hard

Fill both blanks to send a JSON response with a custom status and message.

Express
app.get('/error', (req, res) => {
  res.[1]([2]).json({ error: 'Not found' });
});
Drag options to blanks, or click blank then click option'
Astatus
BsendStatus
C404
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using sendStatus which ends the response immediately.
Using wrong status code like 200 for errors.
Not chaining status before json.
5fill in blank
hard

Fill all three blanks to send a JSON response with a nested object and status code.

Express
app.get('/profile', (req, res) => {
  res.[1]([2]).json({ user: { name: 'Bob', age: [3] } });
});
Drag options to blanks, or click blank then click option'
Astatus
B200
C30
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send instead of res.status for status code.
Putting age in quotes making it a string.
Not chaining status before json.