When you use a PUT request to update data on the server, you need to send all the data for that object, not just the fields you want to change. For example, if you're updating the name and email of a user, you must send the id, and age as well. ... const express = require('express'); const ...
When you use a PUT request to update data on the server, you need to send all the data for that object, not just the fields you want to change. For example, if you're updating the name and email of a user, you must send the id, and age as well. ... const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); // Define a route for PUT requests app.put('/users/:id', (req, res) => { const userId = req.params.id; const updatedUser = req.body; res.json({ message: `User with ID ${userId} updated`, updatedUser }); }); // Start the server app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });An Express server is created and listens on port 3000, with middleware express.json() to handle incoming JSON data in the request body. A sample user object is defined in an array users, which includes id, name, email, and age. The /users/:id PUT route is defined to handle complete replacements of a user's data.Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.The Express server is set up and listens on port 3000, using express.json() middleware to parse incoming JSON request bodies.