- Overview
- Transcript
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
1.1Introduction01:02
1.2Prerequisites06:19
2.Getting Started3 lessons, 30:48
2.1Creating the App Structure11:46
2.2Creating the Server-Side Entry Point10:14
2.3Starting the Angular and Express Apps08:48
3.Setting Up the Mongo Database4 lessons, 27:53
3.1Getting MongoDB Up and Running06:08
3.2Connecting to MongoDB06:47
3.3Creating the Database Schema07:49
3.4Creating a Simple Data Access Layer07:09
4.Creating an API With Express6 lessons, 29:16
4.1Handling Requests in Express09:57
4.2Taking Advantage of the Express Router05:52
4.3Adding the `GET` Handler to the API05:34
4.4Adding the `POST` Handler to the API03:18
4.5Adding the `PUT` Handler to the API02:17
4.6Adding the `DELETE` Handler to the API02:18
5.Building the Front-End Angular App6 lessons, 45:52
5.1Creating the Front-End Models06:57
5.2Creating an Angular Service07:31
5.3Making HTTP Requests From the Service08:33
5.4Setting Up the User Interface09:05
5.5Creating All the Components05:28
5.6Adding Routing to the App08:18
6.Creating the App Components12 lessons, 1:00:02
6.1Adding the View Lawn Markup05:55
6.2Adding the View Lawn Code06:51
6.3Adding the Add Lawn Markup04:34
6.4Adding the Add Lawn Code07:41
6.5Adding the Edit Lawn Markup03:06
6.6Adding the Edit Lawn Code04:11
6.7Adding the View Application Markup02:54
6.8Adding the View Application Code07:46
6.9Adding the Add Application Markup02:16
6.10Adding the Add Application Code04:49
6.11Adding the Edit Application Markup04:20
6.12Adding the Edit Application Code05:39
7.Conclusion1 lesson, 03:18
7.1Conclusion03: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.