Step 9

Let's create a route to receive a specific note given its ID.

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

Notice the API endpoint; to retrieve an item from a collection, it is common to use an endpoint /api/collection/:id where id is the ID of the item you are searching for.

Add the following route to index.js:

app.get("/api/notes/:id", (req, res) => {
  const id = Number.parseInt(req.params.id);
  res.json(notes.read(id));
});

Notice how the path contains :id and how I have used req.params object to get the path parameter (as opposed to using req.query for getting query parameters).

If you want to identify a resource, you should use "path parameter". But if you want to sort or filter items, then you should use query parameter. Source: When Should You Use Path Variable and Query Parameter?

Save the code then head over to your browser and visit http://localhost:4567/api/notes/2. You must receive a JSON object containing the note with ID of 2.

Test this endpoint in Postman too.