Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Express library.
Express
const express = require('[1]');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' instead of 'express' for the import.
Forgetting to put quotes around the package name.
✗ Incorrect
The Express library is imported using require('express').
2fill in blank
mediumComplete the code to define a POST route that expects JSON data.
Express
app.post('/data', (req, res) => { const userData = req.[1]; res.send('Received'); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params or req.query to get JSON data.
Not using middleware to parse JSON before accessing req.body.
✗ Incorrect
The JSON data sent in a POST request is accessed via req.body.
3fill in blank
hardFix the error in the response to send JSON data correctly.
Express
res.[1]({ message: 'Success' });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.send() without setting headers explicitly.
Using res.write() or res.end() which are lower-level methods.
✗ Incorrect
Use res.json() to send a JSON response with the correct headers.
4fill in blank
hardFill both blanks to validate request data and send an error response if invalid.
Express
if (!req.body.name || typeof req.body.name !== '[1]') { return res.status([2]).json({ error: 'Name is required and must be a string' }); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong data type in typeof check.
Using 404 status code which means not found.
✗ Incorrect
The code checks if name is a string and sends a 400 Bad Request status if not.
5fill in blank
hardFill all three blanks to create a response schema that sends user info with id, name, and active status.
Express
res.json({
id: req.body.[1],
name: req.body.[2],
active: req.body.[3]
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong property names that don't exist in req.body.
Including unrelated fields like email when not required.
✗ Incorrect
The response sends userId, name, and active fields from the request body.