Step 12
Let's include a route to delete a note.
| Delete Note | |
|---|---|
| HTTP Method | DELETE |
| API Endpoint | /api/notes/:noteId |
| Request Path Parameter | noteId |
| Request Query Parameter | |
| Request Body | |
| Response Body | JSON object (note) |
| Response Status | 200 |
Notice the path (endpoint) is similar to the GET request we had earlier for retrieving a note given its ID. By convention, we return the deleted note (with status code 200).
Add the following route to index.js:
app.delete("/api/notes/:id", (req, res) => {
const id = Number.parseInt(req.params.id);
const note = notes.delete(id);
if (note) {
res.json(note);
} else {
res.status(404).send("Resource not found!");
}
});
Notice that if the given ID does not exist in the notes collection, the NoteDao returns null. I've accordingly respond with a 404 status to signal "resource was not found".
In Postman, test this endpoint by attempting to delete a note that exist.

Also, test by requesting to delete a note that does not exist.
