Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

Cancel
  • 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.Introduction
2 lessons, 07:21

1.1
Introduction
01:02

1.2
Prerequisites
06:19

2.Getting Started
3 lessons, 30:48

2.1
Creating the App Structure
11:46

2.2
Creating the Server-Side Entry Point
10:14

2.3
Starting the Angular and Express Apps
08:48

3.Setting Up the Mongo Database
4 lessons, 27:53

3.1
Getting MongoDB Up and Running
06:08

3.2
Connecting to MongoDB
06:47

3.3
Creating the Database Schema
07:49

3.4
Creating a Simple Data Access Layer
07:09

4.Creating an API With Express
6 lessons, 29:16

4.1
Handling Requests in Express
09:57

4.2
Taking Advantage of the Express Router
05:52

4.3
Adding the `GET` Handler to the API
05:34

4.4
Adding the `POST` Handler to the API
03:18

4.5
Adding the `PUT` Handler to the API
02:17

4.6
Adding the `DELETE` Handler to the API
02:18

5.Building the Front-End Angular App
6 lessons, 45:52

5.1
Creating the Front-End Models
06:57

5.2
Creating an Angular Service
07:31

5.3
Making HTTP Requests From the Service
08:33

5.4
Setting Up the User Interface
09:05

5.5
Creating All the Components
05:28

5.6
Adding Routing to the App
08:18

6.Creating the App Components
12 lessons, 1:00:02

6.1
Adding the View Lawn Markup
05:55

6.2
Adding the View Lawn Code
06:51

6.3
Adding the Add Lawn Markup
04:34

6.4
Adding the Add Lawn Code
07:41

6.5
Adding the Edit Lawn Markup
03:06

6.6
Adding the Edit Lawn Code
04:11

6.7
Adding the View Application Markup
02:54

6.8
Adding the View Application Code
07:46

6.9
Adding the Add Application Markup
02:16

6.10
Adding the Add Application Code
04:49

6.11
Adding the Edit Application Markup
04:20

6.12
Adding the Edit Application Code
05:39

7.Conclusion
1 lesson, 03:18

7.1
Conclusion
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.

Back to the top