📖 Scenario: You are building a simple Express server to manage a list of books. Users can add, view, and delete books. Now, you will add the ability to delete a book by its ID using a DELETE route.
🎯 Goal: Create an Express DELETE route that removes a book from the list by its id.
📋 What You'll Learn
Create an array called books with three book objects, each having id and title properties.
Create a variable called bookIdToDelete and set it to 2.
Use the filter method on books to remove the book with id equal to bookIdToDelete.
Add an Express DELETE route at /books/:id that deletes the book with the given id from books.
💡 Why This Matters
🌍 Real World
Deleting items from a list via an API is common in web apps, such as removing products from a shopping cart or deleting user posts.
💼 Career
Understanding how to handle DELETE routes in Express is essential for backend web development roles that build RESTful APIs.
Progress0 / 4 steps
1
Create the initial books array
Create an array called books with these exact objects: { id: 1, title: 'Book One' }, { id: 2, title: 'Book Two' }, and { id: 3, title: 'Book Three' }.
Express
Hint
Use const books = [ ... ] with three objects inside.
2
Set the book ID to delete
Create a variable called bookIdToDelete and set it to the number 2.
Express
Hint
Use const bookIdToDelete = 2; to store the ID.
3
Filter out the book to delete
Use the filter method on books to create a new array without the book whose id matches bookIdToDelete. Assign this new array back to books.
Express
Hint
Use books = books.filter(book => book.id !== bookIdToDelete); to remove the book.
4
Add the Express DELETE route
Add an Express DELETE route at /books/:id. Inside the route handler, convert req.params.id to a number, then update books by filtering out the book with that id. Send a JSON response with { message: 'Book deleted' }.
Express
Hint
Use app.delete('/books/:id', (req, res) => { ... }) and inside convert req.params.id to a number, then filter books, then send JSON response.
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
Step 1: Understand HTTP methods
HTTP methods define actions: GET retrieves, POST adds, PUT updates, DELETE removes.
Step 2: Identify DELETE route purpose
DELETE routes specifically handle requests to remove data from the server.
Final Answer:
To remove data from the server -> Option C
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
Step 1: Identify HTTP method for deletion
DELETE method is used to remove resources, so app.delete is correct.
Step 2: Check route path and handler syntax
The path /items/:id with a callback function matches Express route syntax.
C. id is not converted to number before comparison
D. res.send called twice without return
Solution
Step 1: Check flow after 404 response
If product not found, res.status(404).send is called but code continues without return.
Step 2: Identify multiple responses sent
After 404 response, code still filters products and calls res.send again, causing error.
Final Answer:
res.send called twice without return -> Option D
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
Step 1: Check id conversion
id is converted to Number, ensuring correct comparison with book ids.
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.
Step 3: Confirm response handling
If found is true, sends 200 with 'Book deleted'; else sends 404 with 'Book not found'.
Final Answer:
This code correctly deletes the book and handles 404 if not found -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.