Back to Express JS Course

Express JS Course

In Express JS, handling delete requests involves defining a route that handles HTTP DELETE method requests.

import express from "express" const app = express(); const PORT = process.env.PORT || 5000; const mockUsers = [ { id: 1, name: 'Praveen', designation: 'Technical Architect', city: 'Hyderabad' }, { id: 2, name: 'Prasad', designation: 'Technical Lead', city: 'Bangalore' }, { id: 3, name: 'Anusha', designation: 'Lead Developer', city: 'Chennai' }, { id: 4, name: 'Ravi', designation: 'Developer', city: 'Hyderabad' }, { id: 5, name: 'Kiran', designation: 'Technical Lead', city: 'Hyderabad' }, ]; // Middleware to parse JSON bodies app.use(express.json()); // Route to delete a user app.delete('/api/v1_0/users/:id', (req, res) => { const userId = parseInt(req.params.id); const userIndex = mockUsers.findIndex(user => user.id === userId); if (userIndex !== -1) { mockUsers.splice(userIndex, 1); res.status(204).send(); // No Content } else { res.status(404).send('User not found'); } }); app.listen(PORT, () => console.log(`Running on Port ${PORT}`));

Now lets delete an user using Postman.

screenshot of the app

Now lets fetch the users.

screenshot of the app