Approach 2 Usage with Express JS Router
This approach involves creating a base URL using Express JS Router, allowing for a cleaner structure and improved code organization.
Syntax:
const router = express.Router();
router.delete('/', (req, res) => {
// DELETE method logic for the base URL
})
router.delete('/nested', (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; const router = express.Router(); router. delete ( '/' , (req, res) => { res.send( "Deleting Resource" ); }); router. delete ( '/api' , (req, res) => { res.send( "Deleting from /api endpoint" ); }); app.use( '/resource' , router); 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.