0
0
Expressframework~20 mins

DELETE route handling in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DELETE Route Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the response when deleting an existing item?

Consider this Express DELETE route that removes an item by ID from an array. What will the server respond with if the item exists?

Express
const express = require('express');
const app = express();
app.use(express.json());

let items = [{id: 1, name: 'apple'}, {id: 2, name: 'banana'}];

app.delete('/items/:id', (req, res) => {
  const id = Number(req.params.id);
  const index = items.findIndex(item => item.id === id);
  if (index === -1) {
    return res.status(404).send('Item not found');
  }
  items.splice(index, 1);
  res.status(200).send('Deleted');
});
AStatus 200 with body 'Deleted'
BStatus 404 with body 'Item not found'
CStatus 500 with empty body
DStatus 200 with JSON of deleted item
Attempts:
2 left
💡 Hint

Check what happens when the item is found and removed.

📝 Syntax
intermediate
2:00remaining
Which DELETE route syntax is correct?

Which of the following Express DELETE route definitions is syntactically correct?

Aapp.delete('/items/:id', function(req, res) { res.send('Deleted') }
Bapp.delete('/items/:id', (req, res) => { res.send('Deleted'); })
Capp.delete('/items/:id', (req, res) => res.send('Deleted')
Dapp.delete('/items/:id', (req, res) => { res.send('Deleted') )
Attempts:
2 left
💡 Hint

Look for balanced parentheses and braces.

🔧 Debug
advanced
2:00remaining
Why does this DELETE route cause a runtime error?

Given this DELETE route, why does the server crash when a request is made?

Express
app.delete('/users/:id', (req, res) => {
  const id = Number(req.params.id);
  const user = users.find(u => u.id === id);
  users = users.filter(u => u.id !== id);
  res.send(`Deleted user ${user.name}`);
});
ABecause res.send is missing a status code
BBecause users array is not declared
CBecause id is a string but user ids are numbers, find returns undefined causing user.name to fail
DBecause filter does not remove items from the array
Attempts:
2 left
💡 Hint

Check the type of id and how find works.

state_output
advanced
2:00remaining
What is the state of the items array after DELETE request?

Given this initial array and DELETE route, what is the content of items after a DELETE request to /items/2?

Express
let items = [{id: 1, name: 'apple'}, {id: 2, name: 'banana'}, {id: 3, name: 'cherry'}];

app.delete('/items/:id', (req, res) => {
  const id = Number(req.params.id);
  items = items.filter(item => item.id !== id);
  res.status(200).send('Deleted');
});
A[]
B[{id: 1, name: 'apple'}, {id: 2, name: 'banana'}, {id: 3, name: 'cherry'}]
C[{id: 2, name: 'banana'}]
D[{id: 1, name: 'apple'}, {id: 3, name: 'cherry'}]
Attempts:
2 left
💡 Hint

Filter removes items with matching id.

🧠 Conceptual
expert
2:00remaining
What is the best practice for DELETE route response content?

When designing an Express DELETE route, which response practice is recommended for RESTful APIs?

ARespond with status 204 No Content and no response body
BRespond with status 200 and the deleted item in JSON
CRespond with status 404 even if deletion succeeds
DRespond with status 200 and a plain text confirmation message
Attempts:
2 left
💡 Hint

Think about HTTP status codes and REST conventions.