Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you're working with HTTP, get requests are strictly intended for reading data. However, sometimes you need to modify data. In this video we'll configure Express to handle other types of requests. Let's get the exercise files going. Open up chapter 5, video 4, and then copy the start folder to your desktop. Now let's install the modules. Go to Terminal, and then change Directory to that Start folder. I'm going to drag this folder into Terminal to get the path. Now type npm install.
Now I'm going to drag the Start folder onto Sublime Text. Let's take a look at app.js. Currently there is just one route defined. It's the flight number route. Let's add another route we can use for marking specific flights as arrived. Instead of using a get request, I'm going to create a put request using the put method. This path is going to be similar, but I'm going to add arrived to the end. We're using put because we're making changes to data on the server. Let's also change to a different function name.
I'm going to call this arrived. Now, when I first started building web applications, I didn't really pay much attention to the verbs used in HTTP requests. I knew that if I set the method on a form element to get, all of the form data would appear in the URL, but if I set it to post, it wouldn't. I eventually learned more about HTTP verbs and the intent behind them. To learn more about HTTP, I recommend watching David Gassner's web services title in the lynda.com library, particularly the chapter on using Restful Web services.
Now let's go back to the code and create a function to handle that put request. We've already decided we want that function to be named arrived so let's go to routes and index.js and then create a new function called arrived. Just like the get function we're going to receive the request and response objects as arguments. This function is going to be very similar to the get function with a few minor differences. So let's copy the entire body of the get function, and paste it down into arrived. First, we're going to use the trigger arrive method to change the flight record.
And now, instead of sending back the flight information, we're going to send a status. So now, let's go to the server, and test. I'm going to start the server by typing node app, and then return. Looks like there's a small parse error in my code. Let's go back. Instead of typing a semicolon, I typed an l. Let's fix that. Now, let's try starting the server again. So after taking care of the parse error, the server is now listening on port 3000. Just as before, I can load up flight information. So I've loaded up flight 33.
Now, what I need to do is create a put request, so I can mark this flight as arrived. In the past, I've used quick HTML-markup forms to get the browser to help me generate post requests to the server. While this worked for me when I was in a pinch, it's very limiting. You can't use every HTTP verb in a form element, and you can't customize your requests with different headers. Command line tools like Curl are much more flexible. But they tend to be cumbersome if you don't remember all of the flags.
These days, I use a tool called Postman. Now, don't worry. It lets you do far more than post requests. Postman lets you craft HTTP requests exactly the way you want them. Without the need to learn a bunch of command line options for something like Curl. It's available as a free download from the Google Chrome store. So now, lets generate this put request. I am going to copy the URL that I used for the get request and paste it in here. Then I am going to add /arrived to the end of the URL.
Now I am going to change the method to put, now to make this request all I need to do is click the Send button. It kicks back a status of done. And now I can go back to the original record and hit refresh and it'll give me the actual arrive property. Routing different HTTP verbs in Express is as easy as using different methods. As you build out an API, using the appropriate verbs will help other programmers know how to use your service. Routing different HTTP verbs in Express is as easy as using different methods.
As you build out an api using the appropriate verbs will help other programmers know how to use your service. In the next video we'll look at ways of adding functions applying to all HTTP requests.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105289 Viewers
56 Video lessons · 117045 Viewers
71 Video lessons · 86241 Viewers
131 Video lessons · 41225 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.