Lessons: 34Length: 3.4 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.5 Adding the `PUT` Handler to the API

The next operation to add to our Express API is the updating of existing objects. Updates are traditionally handled through a PUT request. So let's add a PUT handler to our API.

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.5 Adding the `PUT` Handler to the API

Onto the updates, so now typically when you're updating an object via an API, you're gonna be using the put method. And then when you're using that put method you're gonna be passing in a couple pieces of data. The first thing that you want to pass in is the ID of the object that you want to be updating. So whatever that unique ID is, and that's typically going to be passed in via the URL. Parameters, using an ID like we've seen before. And then, we're also gonna be passing in a body. So we're doing kinda a combination of some of the requests that we've done so far. And in that body, you're gonna have that updated object, that updated lawn object, that you want to process. So let's go ahead and take care of that now. So we'll say router.put, and remember we wanna be dealing with an ID. So we do need an identifier here. And then we're gonna have our request, response, and next, just like we've been doing so far. And then within here, what wanna do is want to call our Lawn.updateLawn function that we created before. And the first thing we need to do is pass in the unique identifier of which one it is we´re trying to update. And remember that´s gonna come from the params collection, .id. And then we´re gonna need to know what the updated version of that specific lawn that´s represented by that ideas. And that´s coming from the body. And at this point, what you can actually do, because we're expecting that the user or the consumer of this API is actually just sending back a JSON object in the body that's representing this new lawn. So we can simply say req.body, and then we'll go ahead and do our normal processing here for our callback. So now in here we'll do our if err, then we will go ahead and return next err and else. If everything went well, then we'll just go ahead and say res.json. And we can go ahead an say req.body. We'll just go ahead and pass back exactly what it is they sent us. Because if everything goes successfully, then whatever it is that they sent us should have been properly saved. And once it's been saved, that's going to be the updated version of the lawn. We can go ahead and pass that back to the end user or whoever the consumer of this API is.

Back to the top