Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Here's my solution to the challenge. I'm already in terminal, and I'm going to change directory to the solution folder that I already have on the desktop. I'm going to drag this folder in, so that I get the path. Now, I'm going to run my code. I'm going to type node space app.js. On the second to last line it shows how many flights were created. And then on the final line it shows all the destinations served. So let's take a look at how this was done. First, let's take a look at index dot js in Flight.
On lines one and two I'm defining two variables that are local to this module. The first one is count. I'm initially setting this to zero and it's going to keep track of how many flight objects get created out of this module. The second one is destinations. I'm setting this to an empty array and we're going to keep track of each destination as it gets added through this module. Now I'm going to skip over the entire flight function and go down to the bottom. Instead of having module exports I now just have exports and I've attached three functions to it.
The first function is create. This function is very similar to the original one that we had here. The main difference is that on line 41, we're now incrementing the count each time we create a new object. And the other difference is that we're now taking a look at the destination as we create the object. Line 42 is checking to make sure that we don't already have the destination in the array. And if it's not there, we add it on line 43. So these four lines are keeping track of all the data that we want to cache in this module.
On lines 49 through 55, I have a couple of getters. The first one returns the count and then the second one returns the destinations array. Now let's go to app dot js and have a look at the changes there. The first changes are on lines 9 and 21. Instead of just calling flight, we're now calling flight dot create. Then down at the bottom of the file, we're calling get count, and get destinations. This returns the number of flights that have been created, as well as all of the destinations. So it's possible to use nodes module caching to your advantage, while avoiding it when it gets in the way.
Get unlimited access to all courses for just $25/month.Become a member