0
0
ExpressHow-ToBeginner · 3 min read

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 request and response objects.
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.id to a number).
  • Not sending a response, which causes the request to hang.
  • Using app.get or app.post instead of app.delete for 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.

MethodPurposeExample
app.deleteHandles HTTP DELETE requestsapp.delete('/resource/:id', (req, res) => { ... })
req.paramsAccess route parametersconst id = req.params.id
res.statusSet HTTP status coderes.status(200)
res.sendSend response bodyres.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.