List Resources - GET /todos
With our test suite in place, it's now time to create our second route, the GET /todos
route, which will be responsible for returning all of your Todos. This is useful for any Todo application.
Creating the GET /todos route
The first screen you're probably going to show a user is a list of all of their Todos. This is the route you would use to get that information. It's going to be a GET
request so I'm going to use app.get
to register the route handler, and the URL itself is going to match the URL we have, /todos
, because we want to get all of the Todos. Later on when we get an individual Todo, the URL will look something like /todos/123
, but for now we're going to match it with the POST URL. Next up, we can add our callback right above app.listen
in server.js
; this is going to give us our request and response objects:
app.get('/todos', (req, res) => { });
All we need to do is get all of the Todos in the collection, which we've already done in the test file. Inside...