Approach 1 Direct Usage in Express JS

In this approach, we leverage Express JS to handle nested routes and implement the DELETE method, providing a clear and straightforward organization of code.

Syntax:

app.delete('base URL', (req, res) => {
// DELETE method logic for the base URL
})

app.delete('base URL/Nested URL', (req, res) => {
// DELETE method logic for the nested URL
})

Example: Write the following code in server.js file.

Javascript




// Server.js
 
const express = require('express');
const app = express();
const PORT = 3000;
 
app.delete("/resource", (req, res) => {
    console.log("DELETE Request Successful!");
    res.send("Resource Deleted Successfully");
});
 
app.delete("/resource/:id", (req, res) => {
    const resourceId = req.params.id;
    console.log(`Deleting resource with ID: ${resourceId}`);
    res.send(`Resource with ID ${resourceId} Deleted Successfully`);
});
 
app.listen(PORT, () => {
    console.log(`Server established at ${PORT}`);
});


How to test DELETE Method of Express with Postman ?

The DELETE method in web development plays a crucial role in handling resource deletions on the server. As one of the HTTP methods, its primary purpose is to request the removal of a resource identified by a specific URI. In this article, we will see how we can use the Delete method in different scenarios efficiently.

Similar Reads

Prerequisites:

Express.js Framework HTTP Methods (DELETE) Node.js and npm...

Approach 1: Direct Usage in Express JS:

In this approach, we leverage Express JS to handle nested routes and implement the DELETE method, providing a clear and straightforward organization of code....

Approach 2: Usage with Express JS Router:

...

Implementation:

This approach involves creating a base URL using Express JS Router, allowing for a cleaner structure and improved code organization....