Step 10

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

Read Note
HTTP MethodGET
API Endpoint/api/notes/:id
Request Path Parameterid
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 resource you are searching for.

Add the following route to index.js:

app.get("/api/notes/:id", async (req, res) => {
  const { id } = req.params;
  const data = await notes.read(id);
  res.json({ data: data ? data : [] });
});

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

If you want to identify a resource, you should use the "path parameter." But if you want to sort or filter items, then you should use query parameter.

Save the code, and then test this endpoint in Postman.

Resources