Viewers: in countries Watching now:
When you're working with a large set of data, creating a separate route for each record is just not feasible. Fortunately, Express provides a way of handling URLs matching certain patterns. In this video, we'll define routes with a bit more flexibility. Now first, let's copy from the exercise files. Go to chapter five, video three, and copy the start folder to your desktop. Now let's go to Terminal and let's install the modules for this project. Type in cd and space, and then drag the folder into Terminal.
That will get us the path. And now I'm going to hit Return, and then type npm install. So now that we've installed the modules, let's start making some more changes to this application. Now, back in the exercise files, there is a folder named Data. This folder is a module containing flight information. Let's copy this folder into the start folder. Now let's start using this data. I'm going to open the start folder in sublime text. Now let's go to routes index.js. Now keep in mind here, even though we've defined this data as a module, you can power this from a database or any other data source you wanted.
In this case we're just going to use a module, because it's simple. Now let's import this data using Require. I'm going to type var and then data equals require and then I'm going to go down a level and go to slash data. So that will include all of the data in this file as an object. Back in index.js, let's remove the existing two flights. We're not going to use them anymore. Now we're going to process all the data in this file and turn these properites into objects.
To do this we're going to use a loop and then we're going to read through the object properties. So all I'm going to do here is set each flight property into an object created by the flight module. Now let's handle the functions. Shorten flight one to just flight, and then remove flight 2 entirely. So now we're going to use a URL variable to help us find the flight information. We'll be defining this in app.js in a moment. But for now we're going to use number as our variable name. This will allow us to use whatever value number is set to, to determine which flight to load up.
So here, I'm going to type, var number equals req param number. And then, later, in app.js, we'll define where that number comes from. And now, I'm going to use this number to do the lookup. We only want to display data if a valid flight number has been specified. So first, check to see if the flight information is undefined. If it is, spit back a 404 error with a simple status object. Otherwise, we want to fetch the record at that number, and get the information from the flight.
Then we'll send this information back as json. Now that with defined the function for looking up the data and returning it it's time to go back to app.js and define the route. So first, let's get rid of flight 2 entirely. And then let's change flight 1 to /:number. This is where that number variable is being defined. When someone loads a url matching this pattern and specifies a number in this spot. That number is going to be passed to our function.
Also, shorten routes.flight1 to just flight. Now, let's start up the server, and look at a couple of different records. Go back to Terminal, and type node app, and then press return. So, it looks like there's a small problem with my application. On routes/index.js, on line ten, it's not finding that flights variable. Let's go back to my application, and make sure everything's okay. So, on line 10, it's looking for the flights variable.
But the flights variable isn't being defined anywhere before line ten. The data I really want to work with is named data. All I have to do is change data to flights and it will work now. You don't necessarily have to match the name of the module with the name of the variable you use to store the results. Now let's go back to Terminal and try starting the application again. I'm going to press the up arrow on my keyboard and press Return. So now let's load up some flight data. I'm going to go to localhost at port 3000 and then I'm going to go to /flight.
And then /33. So that's now pulling up flight number 33 from LAX to Denver. And now if we go to the data you'll notice that it's the same data that's on line 16 through 22. Let's try loading up flight number 18. Again, what's being output in the browser matches whats in our data set. Defining URL variables makes routes much more flexible. By using variables we don't have to define a seperate route for each and every record in the system. In the next video we'll have a look at how to properly define routes for modifying data.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.