Getting an individual resource – GET /todos/:id
In this section, you are going to create an API route for fetching an individual Todo. Now, most of this section is going to be a challenge but there is one thing I want to show you before we get started, and that is how to fetch a variable that's passed in via the URL. Now, as I mentioned, the structure for this URL is going to be a GET
request, /todos
, then we're going to dive into the Todos, fetching an individual item where the ID gets passed, such as /todos/12345
. This means that we need to make the ID part of the URL dynamic. I want to be able to fetch that value, whatever a user happens to pass in, and use it to make the query. The query that we set up in the mongoose-queries
file like User.findById
one to fetch the todo by Id.
Now in order to get that done, let's go ahead inside server.js
file and call app.get
, passing in the URL.
Taking on the challenge
The first part we already know, /todos/
, but now what we need is a URL parameter....