Bird
Raised Fist0
Expressframework~20 mins

DELETE route handling in Express - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a DELETE route in Express?
easy
A. To update existing data on the server
B. To add new data to the server
C. To remove data from the server
D. To retrieve data from the server

Solution

  1. Step 1: Understand HTTP methods

    HTTP methods define actions: GET retrieves, POST adds, PUT updates, DELETE removes.
  2. Step 2: Identify DELETE route purpose

    DELETE routes specifically handle requests to remove data from the server.
  3. Final Answer:

    To remove data from the server -> Option C
  4. Quick Check:

    DELETE = remove data [OK]
Hint: DELETE routes always remove data from the server [OK]
Common Mistakes:
  • Confusing DELETE with POST or GET
  • Thinking DELETE adds or updates data
  • Assuming DELETE retrieves data
2. Which of the following is the correct syntax to define a DELETE route for path /items/:id in Express?
easy
A. app.delete('/items/:id', (req, res) => { /* handler */ });
B. app.get('/items/:id', (req, res) => { /* handler */ });
C. app.post('/items/:id', (req, res) => { /* handler */ });
D. app.put('/items/:id', (req, res) => { /* handler */ });

Solution

  1. Step 1: Identify HTTP method for deletion

    DELETE method is used to remove resources, so app.delete is correct.
  2. Step 2: Check route path and handler syntax

    The path /items/:id with a callback function matches Express route syntax.
  3. Final Answer:

    app.delete('/items/:id', (req, res) => { /* handler */ }); -> Option A
  4. Quick Check:

    DELETE route syntax = app.delete(path, handler) [OK]
Hint: Use app.delete for DELETE routes, not get/post/put [OK]
Common Mistakes:
  • Using app.get or app.post instead of app.delete
  • Missing the callback function
  • Incorrect path format
3. What will be the response if the following Express DELETE route is called with DELETE /users/5 and the user with id 5 exists?
const users = [{id: 1}, {id: 5}, {id: 9}];
app.delete('/users/:id', (req, res) => {
  const id = Number(req.params.id);
  const index = users.findIndex(u => u.id === id);
  if (index !== -1) {
    users.splice(index, 1);
    res.status(200).send('User deleted');
  } else {
    res.status(404).send('User not found');
  }
});
medium
A. Response status 200 with message 'User deleted'
B. Response status 404 with message 'User not found'
C. Response status 500 with server error
D. Response status 200 with message 'User not found'

Solution

  1. Step 1: Check if user with id 5 exists

    The array contains {id: 5}, so index will not be -1.
  2. Step 2: Analyze response when user found

    Since index !== -1, user is removed and response status 200 with 'User deleted' is sent.
  3. Final Answer:

    Response status 200 with message 'User deleted' -> Option A
  4. Quick Check:

    User exists = 200 'User deleted' [OK]
Hint: If item found, DELETE route sends 200 with success message [OK]
Common Mistakes:
  • Assuming 404 even if user exists
  • Confusing status codes
  • Not converting id to number before comparison
4. Identify the error in this DELETE route code snippet:
app.delete('/products/:id', (req, res) => {
  const id = req.params.id;
  const product = products.find(p => p.id === id);
  if (!product) {
    res.status(404).send('Product not found');
  }
  products = products.filter(p => p.id !== id);
  res.send('Product deleted');
});
medium
A. Incorrect route path syntax
B. Missing callback function in app.delete
C. id is not converted to number before comparison
D. res.send called twice without return

Solution

  1. Step 1: Check flow after 404 response

    If product not found, res.status(404).send is called but code continues without return.
  2. Step 2: Identify multiple responses sent

    After 404 response, code still filters products and calls res.send again, causing error.
  3. Final Answer:

    res.send called twice without return -> Option D
  4. Quick Check:

    Multiple res.send calls without return cause errors [OK]
Hint: Use return after sending response to avoid multiple sends [OK]
Common Mistakes:
  • Not returning after sending 404 response
  • Ignoring type conversion for id
  • Assuming route path is wrong
5. You want to create a DELETE route that removes a book by its ID from a nested data structure where books are grouped by genre. Which approach correctly handles deletion and sends a 404 if the book is not found?
const library = {
  fiction: [{id: 1, title: 'A'}, {id: 2, title: 'B'}],
  nonFiction: [{id: 3, title: 'C'}]
};

app.delete('/books/:id', (req, res) => {
  const id = Number(req.params.id);
  let found = false;
  for (const genre in library) {
    const index = library[genre].findIndex(book => book.id === id);
    if (index !== -1) {
      library[genre].splice(index, 1);
      found = true;
      break;
    }
  }
  if (found) {
    res.status(200).send('Book deleted');
  } else {
    res.status(404).send('Book not found');
  }
});
hard
A. The code does not break after deletion, causing multiple deletions
B. This code correctly deletes the book and handles 404 if not found
C. The code misses converting id to number, causing no matches
D. The code sends 200 status even if book is not found

Solution

  1. Step 1: Check id conversion

    id is converted to Number, ensuring correct comparison with book ids.
  2. Step 2: Verify loop and deletion logic

    Loop checks each genre, finds book by id, deletes it, sets found true, and breaks loop to avoid extra deletions.
  3. Step 3: Confirm response handling

    If found is true, sends 200 with 'Book deleted'; else sends 404 with 'Book not found'.
  4. Final Answer:

    This code correctly deletes the book and handles 404 if not found -> Option B
  5. Quick Check:

    Proper id conversion, deletion, and response = correct code [OK]
Hint: Convert id, break loop after deletion, send 404 if not found [OK]
Common Mistakes:
  • Not converting id to number before comparison
  • Not breaking loop after deletion
  • Sending success response even if book missing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard