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 of server.test.js, we used Todo.find to fetch all of the Todos. We're going to use that same technique right here, but we're not passing in a query; we want to return everything.

app.get('/todos', (req, res) => {
  Todo.find()
});

Later on when we add authentication you'll get back just the Todos you created, but for now, without authentication you're going to get everything in the Todos collection back.

Next up, we're going to attach a then call. This then call is going to take two functions, the success case function when the promise gets resolved, and a function that gets fired when the promise gets rejected. The success case will get called with all of the todos and all we're going to do is send that information back using res.send.

app.get('/todos', (req, res) => {
  Todo.find().then((todos) => {
    res.send()
  }, (e) => {

  })
});

We could pass in the todos array, but this is not the best way to get the job done. When you pass back an array you're kind of locking yourself down. If you want to add on another property, whether it's a custom status code or some other data, you can't because you have an array. The better solution would be to create an object, and on that object specify todos, setting it equal to the todos array using ES6:

app.get('/todos', (req, res) => {
  Todo.find().then((todos) => {
    res.send({todos});
  }, (e) => {

  })
});

This would let you add other properties later on. For example, I could add some sort of custom status code setting it equal to whatever I like. By using an object as opposed to sending an array back, we're opening ourselves up to a more flexible future. With this in place, our success case is good to go. The only thing we need to do to wrap this one up is handle errors, and the error handler is going to look exactly like the one we used previously, res.status. We're going to be sending back a 400 and we'll be sending back the error object that got passed into the function:

app.get('/todos', (req, res) => {
  Todo.find().then((todos) => {
    res.send({todos});
  }, (e) => {
    res.status(400).send(e);
  });
});

Now that we have this in place, we can go ahead and start up our server and test things out over inside of Postman.