In this activity, you'll build an API that manages a list of notes.
Test cases have been defined to guide you through this activity! Test your code with
npm run test.
Once again, we are storing our data as an array in db/notes.js. This time,
notice that we are exporting functions to interact with the data, rather than
the data itself. This is a common practice to limit direct access to the data.
- Complete
getNoteByIdto find the note with the given id. - Complete
addNoteto create a new note with the given text.
Once our data layer is complete, we can move on to serving that data with Express! An Express router is a way to extract related middleware into a separate module which can then be exported and imported into the main app.
- In
api/notes.js, create a new Express router and export it. - Back in
app.js, use body-parsing middleware so that it can parse JSON request bodies. - Import your newly created Express router and use it as the middleware for the
/notesroute. Your app will now prepend/notesto the routes that you are about to define in your router.
Warning
Remember that middleware order matters! Route-handling middleware should live after preprocessing middleware, such as logging middleware or body-parsing middleware. Error-handling middleware should come last, after all other middleware.
- In your router,
GET /should send the array of notes. POST /- If the request does not have a body, send the message "Request must have a body." with status 400.
- If
textis not provided, send the message "New note must have text." with status 400. - If the text is provided, then create the note and send it back with status 201.
GET /:id- Send the note with the given ID if it exists.
- If the note with the ID was not found, send status 404 with a corresponding message.
If everything was done correctly, your app should now be able to handle requests to
GET /notes, POST /notes, and GET /notes/:id. You should pass all test cases!