Get working!
Time to draw the rest of the f'ing owl.
Change the routes in your API:
Hint
Your requests will now be asynchronous, so you'll need to use `async`/`await` or `.then()`/`.catch()` to handle the promises.
You can set the callback as a async function, and then await the database response.
app.get('/books', async (req, res) => {
const books = await db.collection('books').find() //...
// ...
})
- Get a single record
Hint
Remember to change the `:id` parameter from a string to an `ObjectId` this time. To do so, you will need to import it.
import { ObjectId } from 'mongodb'
- Insert new records
Hint
You'll need to read the body of the request. Make sure you have the `express.json()` middleware in place.
app.use(express.json())
Test it out
Test your API using the following curl command:
curl -X POST -H "Content-type: application/json" -d '{"title": "Lord of the Rings", "author": "J.R.R. Tolkien", "pages": 1523, "read": false}' localhost:5050/books
- Update a record
Hint
To update a record, you will need to use the `$set` operator. You can read more about it [here](https://docs.mongodb.com/manual/reference/operator/update/set/).
Test it out
Test your API using the following curl command (changing the last part of the URL to an existing ID):
curl -X PUT -H "Content-type: application/json" -d '{"read": true}' localhost:5050/books/649dd95cbf8cbb6229b09b93
- Delete a record
Test it out
Test your API using the following curl command (changing the last part of the URL to an existing ID):
curl -X DELETE localhost:5050/books/649dd95cbf8cbb6229b09b93