0
0
Expressframework~30 mins

Updating documents in Express - Mini Project: Build & Apply

Choose your learning style9 modes available
Updating Documents with Express
📖 Scenario: You are building a simple web server that manages a list of books. Each book has a title and an author. You want to update the author of a specific book using Express.
🎯 Goal: Create an Express server with a route to update the author of a book by its title.
📋 What You'll Learn
Create an array called books with three book objects, each having title and author properties.
Create a variable called targetTitle with the value of the book title to update.
Use Array.prototype.find() to locate the book object with the title matching targetTitle and update its author property.
Add an Express PUT route at /books/:title that updates the author of the book with the given title.
💡 Why This Matters
🌍 Real World
Updating documents or records is a common task in web servers managing data like books, users, or products.
💼 Career
Understanding how to update data in Express routes is essential for backend development roles.
Progress0 / 4 steps
1
Create the initial books array
Create an array called books with these exact objects: { title: '1984', author: 'George Orwell' }, { title: 'Brave New World', author: 'Aldous Huxley' }, and { title: 'Fahrenheit 451', author: 'Ray Bradbury' }.
Express
Need a hint?
Remember to create an array named books with three objects inside.
2
Set the target book title to update
Create a variable called targetTitle and set it to the string '1984'.
Express
Need a hint?
Use const to create targetTitle and assign '1984'.
3
Find and update the book author
Use const bookToUpdate = books.find(book => book.title === targetTitle) to find the book. Then set bookToUpdate.author = 'Eric Arthur Blair' to update the author.
Express
Need a hint?
Use find() to get the book, then assign the new author.
4
Add Express PUT route to update author
Add an Express PUT route at /books/:title. Inside the route, find the book by req.params.title and update its author with req.body.author. Send back the updated book as JSON.
Express
Need a hint?
Use app.put with req.params.title and req.body.author to update and respond.