Back to Express JS Course
Express JS Course
To handle PATCH requests in Express JS, you typically use the .patch() method of an Express router.
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()); // PATCH endpoint to update user by ID app.patch('/api/v1_0/users/:id', (req, res) => { const id = parseInt(req.params.id); const updatedUser = req.body; // Find the user by ID const userToUpdate = mockUsers.find(user => user.id === id); // If the user is found, update its properties if (userToUpdate) { userToUpdate.name = updatedUser.name; userToUpdate.designation = updatedUser.designation; userToUpdate.city = updatedUser.city; res.status(200).json({ message: 'User updated successfully', user: userToUpdate }); } else { res.status(404).json({ message: 'User not found' }); } }); app.listen(PORT, () => console.log(`Running on Port ${PORT}`));
Now lets patch an user using Postman.

Now lets fetch the users.
