New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

Node.js Essential Training
Illustration by Richard Downs

Creating modules with getters and setters


From:

Node.js Essential Training

with Joseph LeBlanc

Video: 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.
Expand all | Collapse all
  1. 3m 9s
    1. Welcome
      42s
    2. What you should know before watching this course
      57s
    3. Using the exercise files
      47s
    4. Using the challenges
      43s
  2. 18m 50s
    1. Why use Node.js?
      2m 1s
    2. Choosing a Node.js installation process
      4m 56s
    3. Installing Node.js on the Mac
      2m 5s
    4. Installing Node.js on Windows
      1m 43s
    5. Installing Node.js via Node Version Manager on Ubuntu Linux
      4m 45s
    6. Installing Node.js via Node Version Manager on a Mac
      3m 20s
  3. 13m 54s
    1. Exploring language additions to the V8 JavaScript engine
      3m 38s
    2. Understanding require() and modules
      6m 39s
    3. Understanding callback execution in the event loop
      3m 37s
  4. 12m 23s
    1. Initializing Node.js projects
      2m 37s
    2. Finding an NPM
      3m 30s
    3. Maintaining projects using the npm command
      6m 16s
  5. 21m 4s
    1. Creating modules with getters and setters
      6m 33s
    2. Understanding module caching and scopes
      6m 15s
    3. Implementing JavaScript creational patterns
      5m 8s
    4. Challenge: Leveraging module caching
      55s
    5. Solution: Leveraging module caching
      2m 13s
  6. 32m 27s
    1. Starting Express applications
      3m 42s
    2. Handling GET requests returning JSON
      3m 26s
    3. Navigating large datasets using request variables in routes
      5m 4s
    4. Modifying data through PUT requests
      4m 32s
    5. Supplying middleware to modify HTTP response headers
      4m 37s
    6. Generating HTML views in Jade
      8m 9s
    7. Challenge: List all records as JSON
      37s
    8. Solution: List all records as JSON
      2m 20s
  7. 25m 37s
    1. Evaluating unit testing methodologies
      3m 8s
    2. Isolating existing code for testing
      6m 47s
    3. Supplying helpers for test data
      2m 35s
    4. Writing unit tests with Mocha
      6m 5s
    5. Simulating HTTP requests with SuperTest
      7m 2s
  8. 28m 45s
    1. Connecting to MongoDB via Mongoose
      4m 16s
    2. Defining schemas for collections in Mongoose
      3m 47s
    3. Querying MongoDB collections
      5m 32s
    4. Persisting Express sessions through MongoDB and mongo-connect
      7m 6s
    5. Logging in to Express applications using Passport
      8m 4s
  9. 21m 47s
    1. Controlling the built-in REPL
      4m 18s
    2. Constructing a custom REPL
      5m 40s
    3. Receiving command-line arguments with Optimist
      6m 14s
    4. Building command-line tools
      3m 35s
    5. Challenge: Launch a custom REPL from a command
      41s
    6. Solution: Launch a custom REPL from a command
      1m 19s
  10. 15m 46s
    1. Emitting events and attaching listeners
      5m 32s
    2. Streaming chunked data through readable streams
      2m 33s
    3. Controlling readable streams
      1m 52s
    4. Piping readable data into writable streams
      2m 10s
    5. Handling duplex streams over TCP
      3m 39s
  11. 4m 6s
    1. Deploying projects to cloud hosting services
      2m 36s
    2. Finding Node.js resources
      1m 30s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Node.js Essential Training
3h 17m Intermediate Oct 16, 2013

Viewers: in countries Watching now:

Node.js is a powerful tool for controlling web servers, building applications, and creating event-driven programming. And it brings JavaScript—a language familiar to all web developers—into an environment independent of web browsers. Learn all about Node.js and start creating JavaScript applications in this course with Joseph LeBlanc. He shows how to install Node.js on Mac, Windows, and Linux and dives deep into its modules and Express framework for app development. Learn how to unit test your code, start sessions on web servers, stream data, and create simple command-line tools.

Topics include:
  • 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
Subjects:
Developer Servers Cloud Computing Programming Languages
Software:
JavaScript Node.js
Author:
Joseph LeBlanc

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.

There are currently no FAQs about Node.js Essential Training.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Node.js Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.