0
0
Expressframework~10 mins

DELETE route handling 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 create a DELETE route for '/items/:id'.

Express
app.[1]('/items/:id', (req, res) => {
  res.send('Delete item ' + req.params.id);
});
Drag options to blanks, or click blank then click option'
Adelete
Bget
Cput
Dpost
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.get instead of app.delete
Using app.post or app.put for delete routes
2fill in blank
medium

Complete the code to extract the 'id' parameter from the DELETE route.

Express
app.delete('/items/:id', (req, res) => {
  const itemId = req.[1].id;
  res.send('Deleting item ' + itemId);
});
Drag options to blanks, or click blank then click option'
Abody
Bquery
Cheaders
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body to get route parameters
Using req.query for route parameters
3fill in blank
hard

Fix the error in the DELETE route to send a 204 status code with no content.

Express
app.delete('/users/:userId', (req, res) => {
  // Delete user logic here
  res.[1](204).send();
});
Drag options to blanks, or click blank then click option'
AsendStatus
Bstatus
Cjson
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using res.sendStatus(204) which ends the response and cannot be chained with .send()
Using res.json() which sends a JSON body
4fill in blank
hard

Fill both blanks to create a DELETE route that checks if the item exists and sends appropriate status.

Express
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();
});
Drag options to blanks, or click blank then click option'
Aparams
Bbody
Cstatus
DsendStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body instead of req.params for id
Using res.sendStatus(404) which sends default message without custom text
5fill in blank
hard

Fill all three blanks to create a DELETE route that validates the id, deletes the item, and sends a JSON response.

Express
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' });
});
Drag options to blanks, or click blank then click option'
Aparams
Bstatus
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body instead of req.params for taskId
Using res.send instead of res.json for JSON response
Using res.sendStatus which sends default text instead of JSON