Update a Book By ID (PUT)
When you want to update a specific book, you provide the book’s unique ID along with the new data in the request. The API then locates the book with the given ID in the database and replaces its existing information with the new data.
Javascript
// Update a book by ID app.put( '/books/:id' , async (req, res) => { try { const book = await Book.findByIdAndUpdate( req.params.id, req.body, { new : true } ); if (!book) { return res.status(404) .json({ error: 'Book not found' }); } res.json(book); } catch (error) { res.status(500) .json({ error: error.message }); } }); |
How to Build a RESTful API Using Node, Express, and MongoDB ?
This article guides developers through the process of creating a RESTful API using Node.js, Express.js, and MongoDB. It covers setting up the environment, defining routes, implementing CRUD operations, and integrating with MongoDB for data storage, providing a comprehensive introduction to building scalable and efficient APIs.
Contact Us