How to Use app.delete in Express: Syntax and Example
In Express, use
app.delete(path, callback) to handle HTTP DELETE requests at a specific path. The callback function runs when a DELETE request matches the path, allowing you to define how to respond.Syntax
The app.delete method defines a route handler for HTTP DELETE requests. It takes two main parts:
- path: The URL path to listen for DELETE requests.
- callback: A function that runs when a DELETE request matches the path. It receives
requestandresponseobjects.
javascript
app.delete(path, (req, res) => { // handle delete request });
Example
This example shows how to create a simple Express server that listens for DELETE requests on /items/:id. It simulates deleting an item by ID and sends a confirmation response.
javascript
import express from 'express'; const app = express(); const port = 3000; // Sample data let items = [{ id: 1, name: 'Book' }, { id: 2, name: 'Pen' }]; app.delete('/items/:id', (req, res) => { const id = parseInt(req.params.id, 10); items = items.filter(item => item.id !== id); res.status(200).send({ message: `Item with id ${id} deleted.` }); }); app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); });
Output
Server running on http://localhost:3000
// When DELETE /items/1 is called, response: { message: "Item with id 1 deleted." }
Common Pitfalls
Common mistakes when using app.delete include:
- Not parsing route parameters correctly (e.g., forgetting to convert
req.params.idto a number). - Not sending a response, which causes the request to hang.
- Using
app.getorapp.postinstead ofapp.deletefor DELETE requests.
javascript
/* Wrong: Not sending response */ app.delete('/items/:id', (req, res) => { // Deletes item but forgets to respond items = items.filter(item => item.id !== parseInt(req.params.id, 10)); // Missing res.send or res.end }); /* Right: Always send a response */ app.delete('/items/:id', (req, res) => { items = items.filter(item => item.id !== parseInt(req.params.id, 10)); res.status(200).send({ message: 'Deleted successfully' }); });
Quick Reference
Use app.delete(path, callback) to handle DELETE requests. Always parse parameters carefully and send a response to avoid hanging requests. This method is part of Express routing to manage RESTful APIs.
| Method | Purpose | Example |
|---|---|---|
| app.delete | Handles HTTP DELETE requests | app.delete('/resource/:id', (req, res) => { ... }) |
| req.params | Access route parameters | const id = req.params.id |
| res.status | Set HTTP status code | res.status(200) |
| res.send | Send response body | res.send({ message: 'Deleted' }) |
Key Takeaways
Use app.delete(path, callback) to handle HTTP DELETE requests in Express.
Always parse route parameters correctly, especially IDs from req.params.
Send a response in the callback to complete the request and avoid hanging.
app.delete is essential for REST APIs to delete resources cleanly.
Test DELETE routes with tools like Postman or curl to verify behavior.