Step 3
Phillip Hauer has a great blog post API Design Best Practices. We have already employed some and we will employ two more here (and a few other practice in later modules).
One of the recommendations is "Wrap the actual data in a data
field". Let's refactor routes/note.js
to incorporate this practice:
Find every instance of res.json()
and update it to wrap its argument in a data
object as in the example below:
- res.json(note);
+ res.json({ data: note });
Once done, save the file and rerun the API requests in Postman. Here is an example for retrieving a note given its ID.
Similarly, we are going to wrap error messages in an errors
object. In the blog post, this recommendation is made under "Provide Useful Error Messages". Here is an example:
- res.status(404).send("Resource not found!");
+ res.status(404).json({
+ errors: [
+ {
+ status: 404,
+ detail: "Resource not found!",
+ },
+ ],
+ });
You must update all instances where the response entailed an "error" to adhere to the pattern above.
Once done, save the file and rerun the API requests in Postman. Here is an example for attempting to delete a note given an invalid ID.