From the course: Node.js Essential Training
Unlock the full course today
Join today to access over 24,600 courses taught by industry experts.
Creating a module - Node.js Tutorial
From the course: Node.js Essential Training
Creating a module
- [Instructor] A powerful tool that ships with Node.js is the event emitter. The event emitter is Node,js' implementation of the pub-sub design pattern, and it allows us to create listeners for and emit custom events. So let's take a look at it. We're going to do so in this empty file called app js. The first thing we'll do is require events here, and then we're going to create emitter and set that equal to a new events.EventEmitter. Perfect. The next thing we'll do is we're going to use that pattern that we've seen a lot of. We'll say emitter.on, then we will create a custom event and add a callback function here that's going to log whoever the message and the user is. So here we'll say console log, user and message. Excellent, so once we've created this event, we need to emit this event, which we'll do with emitter.emit. We'll use the name of our custom event. We'll use the same string, customEvent, and…
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)
-
-