From the course: Node.js Essential Training
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Exporting custom modules - Node.js Tutorial
From the course: Node.js Essential Training
Exporting custom modules
- [Instructor] Every JavaScript file is a module. We've been loading modules with the require function, and the require function is part of that common JS module pattern, but it only represents half of the pattern. The half that loads the modules. The other half of the pattern is module.exports or the mechanism by which we make our modules consumable. The first thing I'll do here in 03_04's start is I'm going to create a file called myModule.js, and in it we're going to create a few variables. So we're going to create a count variable. We'll set that equal to zero and a few different functions. So the first we'll be for incrementing, and this is going to increment our count, add one to our count, and then we'll also create a decrement function that is going to do the opposite. So --count will remove one, then we'll create this getter function that's going to get whatever the most recent count is. Perfect, so now what I can…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Understanding core modules3m 6s
-
(Locked)
Collecting information with readline1m 38s
-
(Locked)
Using readline4m 58s
-
(Locked)
Exporting custom modules3m 17s
-
(Locked)
Creating a module3m 40s
-
(Locked)
Custom events with the EventEmitter1m 45s
-
(Locked)
Consuming a module with EventEmitter2m 22s
-
(Locked)
-
-