4.3 Adding the `GET` Handler to the API
Now that we have our routing structure in place, it's time to bring in the data access layer. In this lesson, we'll create the two
GET requests that our app needs to handle in order.
1.Introduction2 lessons, 07:21
2.Getting Started3 lessons, 30:48
3.Setting Up the Mongo Database4 lessons, 27:53
4.Creating an API With Express6 lessons, 29:16
5.Building the Front-End Angular App6 lessons, 45:52
6.Creating the App Components12 lessons, 1:00:02
7.Conclusion1 lesson, 03:18
4.3 Adding the `GET` Handler to the API
Now, that we have our basic route structure in place, let's go ahead and fill in the gets for our application. So we're gonna have two different ones. The first one is gonna be a generic get that's gonna return the entire list or all of the available lawns that are within our application. And then we'll also get the individual version, where we can get a single lawn-based on a unique identifier. So here I am in our lawns.js file within our routes folder. And I can get rid of this little demo version right here since we're not really gonna be using that. So we're gonna go ahead and use router. But before we can actually do that, we're gonna need to go into and get our lawn model that we created in our previous lesson. So I can say const Lawn =, and then we'll go ahead and require and bring in that model. So we can say, models/lawn. Now, remember, we are now in a different folder, we're in another subfolder. So we have to come back a level into the root of the folder, then go into models, and get lawn. So now we have lawn. Now, we can start to work with this a little bit more. So we're gonna do our get, remember, this is gonna be a generic get. So this is gonna be just at kind of the root within that lawn's URL structure. And then we're gonna go ahead and do our function. So we have a request, response, and next. And let's go ahead and give the body. Now, interestingly enough, this is gonna be pretty simple based on the work that we've already done. So now what I can do, is I can say, Lawn .getAllLawns. And then from here, I can go ahead and pass in that callback that I can complete once I'm done retrieving all of the lawns. So this is going to take in two parameters, the error and the lawns. So if this is unsuccessful, we get an error, then error is gonna have a value. If we are able to complete this request successfully, we will get a collection of lawns right here. So we're gonna go ahead and fill out this body as well. And so what do we wanna do in this case? So if error, which means if something bad has happened, then in this case, we're actually gonna take advantage of this next function right here. So we'll simply say return next(err). So all we're really gonna do is we're going to say, if something bad happens, then just pass this to the next handler within the pipeline and pass that error along. Else, we go ahead and do a response. Now, what we have done so far before is we've done a send. And that's typically fine, but really all that it's doing is sending an object or whatever it is, some text back to where it came from. But we really don't wanna do that. At this point when we're working with APIs, the response is typically in the form of a JSON object. So luckily for us, there is a helper function to do that, which is .json, and then we can simply pass in lawns. And that will take the structure that we've created with this lawns and then we'll go ahead and pass that as a JSON object back to where we came from. So that's all we really have to do, that's the entire function right there. So it's pretty simple. So let's go ahead and knock out the other version as well this time as well. So now, we've got get, and this time, remember, we wanna be dealing with an individual one, so we need to be talking about an ID. So we're gonna be getting an individual lawn based on that unique ID. And once again, we're gonna have our request, response, and next. And then we'll go ahead and fill in the body. And this is gonna be pretty similar. We're gonna use our getLawn function that we created earlier. And once again, we are going to need to pass some data in. In order to do this, in order to retrieve that specific lawn, we need to pass in an ID. So where does that live? Well, just like we talked about before, we can go into the request, into params, and no we can use that ID value that was passed in. And we're also gonna pass in a similar callback. So we have that same error and lawn kind of structure going on here. And then we have the body of what it is exactly we want to do. So we'll do something very similar. We'll say if there's an error, then we will go head and return next(err), just passing it along down the pipeline. But if it's successful, then we'll simply say, response.json(lawn), just like that. So those are gonna be our two gets. So once again, we have our generic get, that's gonna go at the route, and we're going to get all of our lawns. And if it doesn't work out, then we're simply gonna pass that down the pipeline. But if we do get the lawns, then we'll just go ahead and return the JSON structure of that collection. And then for the individual version, we're gonna be passing in that ID, once again, getting our three parameters. And then we're going to be using the getLawn function, passing in the individual ID from the parameter, from the URL. And then once again, if there's an error, then we'll go ahead and just pass it down the pipeline. But if we did get that individual lawn, then we'll go ahead and send the JSON representation of that lawn back to where we came. So there we go, there we have our two get functions, and those are ready to go, and now we can start to take advantage of those. And in the next lessons, we're gonna kinda finish things up here by doing the post to create a new lawn and update to update a lawn. And then obviously, the delete to delete an individual lawn.