Step 12

Let's include a route to delete a note.

Delete Note
HTTP MethodDELETE
API Endpoint/api/notes/:noteId
Request Path ParameternoteId
Request Query Parameter
Request Body
Response BodyJSON object (note)
Response Status200

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.