Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
Use res.json() to send a JSON response in Express.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
res.json() sends the JSON response after setting the status code.
3fill in blank
hardFix 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'
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.
✗ Incorrect
Age should be a number, not a string, for proper JSON data types.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Use res.status(404) to set status before sending JSON error message.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Use res.status(200) to set status and send JSON with age as number.