Step 9
Here is the handler for the HTTP POST request through /api/notes
endpoint:
router.post("/api/notes", (req, res) => {
const content = req.body.content;
const author = req.body.author;
notes
.create(content, author)
.then((note) => res.status(201).json({ data: note }))
.catch((err) => errorHandler(res, 400, err));
});
Notice if any error arises in the process of creating/storing a new note, it will be captured by the "catch" block.
A better implementation for this handler would be one that looks at the err
object and distinguishes between issues related to e.g. database connection (which must be reported as "Internal Server Error" with status code of 500
) vs. issues related to client's request (which is reported as 400
error: bad request).