Complete the code to create a DELETE route for '/items/:id'.
app.[1]('/items/:id', (req, res) => { res.send('Delete item ' + req.params.id); });
The DELETE method is used to handle delete requests in Express. Use app.delete to define a DELETE route.
Complete the code to extract the 'id' parameter from the DELETE route.
app.delete('/items/:id', (req, res) => { const itemId = req.[1].id; res.send('Deleting item ' + itemId); });
Route parameters like ':id' are accessed via req.params in Express.
Fix the error in the DELETE route to send a 204 status code with no content.
app.delete('/users/:userId', (req, res) => { // Delete user logic here res.[1](204).send(); });
To send a status code with no content, use res.status(204).send(). The sendStatus method sends the status and ends the response (with empty body for 204).
Fill both blanks to create a DELETE route that checks if the item exists and sends appropriate status.
app.delete('/products/:id', (req, res) => { const id = req.[1].id; if (!database.has(id)) { return res.[2](404).send('Not found'); } database.delete(id); res.status(204).send(); });
Use req.params to get the id from the URL. Use res.status(404).send() to send a 404 Not Found status with a message.
Fill all three blanks to create a DELETE route that validates the id, deletes the item, and sends a JSON response.
app.delete('/tasks/:taskId', (req, res) => { const id = req.[1].taskId; if (!isValidId(id)) { return res.[2](400).json({ error: 'Invalid ID' }); } deleteTask(id); res.[3](200).json({ message: 'Task deleted' }); });
Use req.params to get the taskId. Use res.status(400).json() to send a 400 error with JSON. Use res.status(200).json() to send success message in JSON.