Creating modules with getters and setters
Video: Creating modules with getters and settersModules help sort applications into risible pieces. In this video, we'll build a module to keep track of flight information. So to do that, let's create a folder called Flight. Now that we have this folder let's head to the command line. Earlier on we created some projects. Using npm net. And we're going to use that same command again to help us create this module. So to do that, let's change directory into the folder that we just created. Type in CD and space. And then, I'm just going to drag this folder into terminal so I can get that path.
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Creating modules with getters and setters
Modules help sort applications into risible pieces. In this video, we'll build a module to keep track of flight information. So to do that, let's create a folder called Flight. Now that we have this folder let's head to the command line. Earlier on we created some projects. Using npm net. And we're going to use that same command again to help us create this module. So to do that, let's change directory into the folder that we just created. Type in CD and space. And then, I'm just going to drag this folder into terminal so I can get that path.
Now, hit Return, and you're in that folder. So now, let's use that npm init command again. So, type in npm init, and then press Return. We're going to name this module, flight. So just accept flight. Accept version number zero. For the description, I'm going to say, a module for keeping track of a flight. I'm also going to accept index.js as the entry point. And then for test, and git repository and keywords just leave those blank. For the author enter in your own name and then your email address in angle brackets.
I'm just going to accept the BDS-2 Clause license for my module. You can add any license you wish here. Finally it's going to present everything back up here on the screen for us to review before Before it actually creates that package.json file. Everything looks good to me so I'm going to hit Return to accept it. Now let's open this folder in Sublime text and have a look at this package.json file. You'll notice again on line five we have a main property. This property tells node which file to load first when it loads this module.
In this case, index.js is that file. So let's create it. I'm going to create it from the command line. So to do that, I'm going to use the touch command. So I'm going to go touch, and then space. And then index.js. And then when I come back over here to Sublime text, index.js appears in my folder. So now in index.js, I'm going to start writing out this module. For now we're going to keep track of a few pieces of data about a flight. We have a number, an origin and a destination. We're just going to keep it at that for now. So on line one let's create some variables for these pieces of information.
I'm going to do var and that is going to ensure that these variables stay in the scope of this module. These won't be global variables. So now I'm going to type number, and origin and destination. Now I'm going to create three functions to set each of these variables. These are commonly referred to as setter functions. So the first one is going to be setNumber. So first I use exports because I want to export this function so that other pieces of code can use it. And I'm going to call this setNumber. So setNumber is going to be set as a function.
And it's going to take one argument. That's the number that we want to set. So here, I'm just going to set number to num. And then I want to create two more setters. One for origin, one for destination. So just copy that first one, and start modifying it. One thing to note here is that we're using shortened versions of the variable names. Because if we use number here, it's going to conflict with the number that we already have defined up at the top of the file. And now, I'm going to do the same for destination. Now I have three functions here that are setting the variables, but I don't have a function that's going to let me get all that information back out of the module.
So to do that, we need to define a getter function. I'm going to call it getInfo. This function is going to do one thing. It's just going to return an object literal, and we're going to fill that object literal with the values in this module. So we're going to have the number, and in this case we can use the same name because this is a separate label, and then I'm going to use origin, and then finally the destination. So now we have a module that's capable of setting and retrieving data.
So let's use this module in an application. To do that I'm going to go back up to the desktop. So I'm going to type cd, space, and then double dot. And that gets me back to the desktop. Next, I'm going to create a file that's going to load the module that I just created and then use it. So to do that, I'm going to touch app.js. Now let's load app.js in Sublime Text. The first thing I'm going to do is load the flight module. So I'm going to type var so that this is a local variable to this file.
And I'm going to call this variable flight, and then I'm going to require the flight module. Next, I'm going to set an origin, a destination, and a number for this flight. And now that we've set all that data, we can get it all back out of the module by using the getInfo method that we defined earlier. So to do that, I'm going to type console log, because I just want to display all this information back out on the node console. And then I'm going to call flight, and then getInfo. So, now let's run this application. Go back to the command line, type node space and then app.js.
All three pieces of data got sent, and then they came back out of the module. Now, let's take one more look at this module. Go back to package.json, now one thing to note is that the main portion of package.json really does matter here on line five. This is currenttly pointing to index.js well, let's rename the index.js file to say module.js. Now let's go back to the command line and try running it again. I'm going to do this by pressing the Up arrow on my keyboard and hitting Return.
Oops, the app comes crashing down. If you don't want to use index.js as your main file you can change that property. So let's go back to our project and change index.js to module.js on line five. Now let's go back to the command line and try this again. I'm going to press the Up arrow again and then Return and now the code works. Now you don't necessarily have to use index.js as your main entry point, but I typically prefer to. It's easier for me to identify, so let's change it back and try it again.
I'm going to change module.js back to index.js and now I'm going to go back to package.json and change to main back to index.js. And now back to terminal, press up and enter. And the code is working again. All the code independanciues for this module are self contained in the flight folder. When the folder is loaded through Require, the package.json file tells Node which file to load up. In the next video, we'll look at how Node caches modules loaded through Require.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.