Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,974 courses, including more Developer and personalized recommendations.Start Your Free Trial Now
- View Offline
- 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
Skill Level Intermediate
When you're building a large application of any sort, you want to break it down into logical pieces. In this video, we'll take a look at how Node allows us to do this using modules. First, go to the Exercise files and go to Chapter 2, Video 2, then copy the Start folder to the Desktop. Once you have that folder there, open it up in a Text Editor, and then, also, go to the Command Line. I'm going to change directory to Desktop with a D, and then start. If I type in LS, it's showing all of the files and folders that are in that folder.
So now we're ready to start exploring this code. Open up example1.js. At the top, on line 1, we're using that require function that we looked at in the previous video. This code is passing. /announce as the argument for require. What this is doing, is looking for a file called announce.js in our current directory. Since it's specified announce it's going to pull up announce.js here on the same level. So inside that file is all the code that's being exported when we call require.
This function takes one argument, and that argument is expected to be a string. And then finally, we're just calling console.log and then just adding the announcing string onto the beginning of the string that gets passed in. Now, let's go back to example1.js. Since we've exported a function, when we call require and set announce, announce gets set as a function. And then finally, on line 3, we can call announce, and pass in a string. Now, let's go to the Command Line and run this code. Type in node, space, example1.js.
So the output was announcing colon space and then string we passed in, Node essential training. Now let's take a look at a slightly different example. Open up example2.js. Just like in example1, we're calling require on the first line. This time we're passing in. /relay, and then we're setting the results to the relay variable. Let's go take a look at the code for relay. Now in this case there is not relay.js file, but there is a relay folder. So let's open that folder. Inside the relay folder is an index.js file.
Whenever you pass a path into require, it will look for either a. js variant of that name, or it will look for a folder that has an index.js file in it. So let's take a look at this index.js file now. On line 1, we're defining a variable called prefix. We are setting that prefix to the string relaying, colon and space. Now just like in announce.js, we're exporting a function, and this function takes a string. Then on line 4 working cat needing the prefix, the message. Now there's one problem with this code.
On line 1, we're just declaring prefix as a variable. Now just like in java script in a browser this is actually ending up as a global variable. The problem with a global variable, is that it can be overwritten anywhere in the code base. So back over here, we're actually overriding that prefix here on line 3. So instead of saying relaying as we have it defined in line 1 on index.js, it's going to say attention that's defined here on line 3. So now when relay gets called on line 5, it's going to say attention, ticket counter closes at 10 PM.
So let's run this code now. Type in node, space, example2.js, and now it's saying, attention, ticket counter closes at 10 PM. Let's fix this code now. Go back over to index.js, and instead of having it just be prefix, type in var space prefix. By typing in var, this is going to define prefix as a local variable. This way the prefix variable won't get clobbered by other variables named prefix elsewhere in our code base. Save the file, and let's go back to the Command Line.
I'm going to press the Up Arrow to bring up the last command. And now, I'm going to press Enter. Now, the output says, relaying. If we go back to example2, you'll notice that we're still setting the global prefix to say, attention. However, since we're now using a local variable here in the module, it doesn't matter. Now let's take a look at example3. Just like the other two examples, we're calling require and looking for a local module called proclaim. This is going to match up to proclaim.js. Back here in example3 however, you'll notice that we are calling it differently.
The say variable is being set to an object, and this object has two properties, softly and loudly. They're both functions. If we go back to proclaim.js, you'll notice that we're setting exports.softly and exports.loudly. We're not calling module exports because we want to set these individual properties that we're exporting. Another thing you'll notice, is that we're first declaring whisper as a function, and then we're setting softly to the value of whisper. When we go to use this module, the only thing that matters is what the name of the properties of exports are set to.
So now, let's go to the Command Line and run example3. Type in node, example3.js. Finally, let's have a look at example4. Like the other three examples, we're calling require. But this time, we're not specifying a dot and a slash. This is telling Node that we want to use a core module or we want to use a module that's been installed specifically for our project. In this case, the os module is a core module that ships with Node. We're setting the results to the variable os.
Then we're calling the type method of the os object. This is going to tell us what operating system the code is running on. So let's go to the Command Line and run this code now. Type in node example4.js. It's now saying that this code is running on Darwin, and Darwin is the operating system that Mac OS 10 is built on top of. Again, we're not using a dot and a slash to require the os module. Since it's a core module, we can call it from any file without worrying about the path. Modules form fundimental building blocks for programs running in Node.
The give you a way of arranging code into convenient, self contained objects. Coming up next we'll look at how the event loop keeps your Node programs running.