Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Express is designed to make it easy to define and handle HTTP routes. In this video, we'll define several routes and use the module from the previous chapter to handle them. First, let's go to the exercise files. Go to video two, and then copy the start folder onto the desktop. Now I'm going to change directory to this folder. I'm going to type cd space, and then I'm going to drag this folder into terminal. Since I'm pulling these from the exercise files, I need to install the node modules, and to do that, I'm going to type npm, space, install.
So now the node modules are installed. Now let's start building out this application. In the exercise files and video two, there's a folder named flight. This is the module that we developed earlier in the course. I'm going to copy this folder and paste it into start. Now let's edit the code. Open the folder in Sublime Text or any other text editor. First, let's go to routes, and index dot js. The first thing we want to do is get that flight module. So make a little bit of room, and then type var, flight, and then an equals sign, and then require, and then a double dot, / flight.
We're using double dot here because we need to go up one level in the directory tree. Now let's create a couple of objects based on the flight module. Go back to the exercise files and open up snippets.js. Copy everything from line three to line seventeen. And then paste it beneath line six. Now we have two flights, flight one and flight two. They're both based off the flight module that we've included in this project. Next, let's define some functions that will return the flight information to the browser.
I'm going to change exports dot index to exports dot flight1 and then instead of calling a render method I'm going to call the json method. And then finally, I'm going to pass in the flight information. To get that flight information, I'm going to type flight1.getInformation, and then I'm going to call that function. I'm going to do the exact same thing for flight two. Now let's define some routes to call these functions. Go to app.js, and then, change app.get slash, to flight one.
And now use the flight one function that we've defined in the routes object. Do the same for flight two. Now let's start the server, and go to these routes. Go to terminal and type in node, space, app. And then press return. So now the server is listening on port 3000. Let's go to localhost@port3000, and then /flight1. It's currently displaying all of the flight information in json format. Now if you work with json data on a regular basis, I recommend installing the jsonview extension for Google Chrome.
It's what's formatting all the json data here with the coloring. It's available as a free download from the Google Chrome store. Let's also take a look at flight two. Again, it's displaying the flight information in json format. The airline management application now handles a couple of statically defined flights at specific URL's. While this is helpful for getting a handle on how Express works, it's not very scalable for handling larger sets of data. In the next video, we'll look at more flexible ways of handling an indefinite number of flights.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98552 Viewers
61 Video lessons · 85838 Viewers
71 Video lessons · 69736 Viewers
56 Video lessons · 102043 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.
Your file was successfully uploaded.