Getting started with Node.js — Day 2

Amitesh Kumar
Web For You
Published in
4 min readJun 18, 2020

To give you a reference, I wrote my first blog on module.exports in getting started with node.js Day 1 blog.

Here is the link to the previous blog if you haven’t read it. It's a two-minute read do check out :

Now moving to Day 2, Here is the summary of what I learned today(day 2) of my starting with node.js.

Today we are going to learn about the “Events and Event Module Emitters”.

What is event and event module emitters?

  • Most of the node.js API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects(called “emitters”) emit named events that cause ‘Function’ objects(“called listeners”) to be called.

For example, if we are file system module in node.js as fs.ReadStream it emits an event when the file is opened. I will give a detailed blog on file system modules and how to work with it in the upcoming blogs.

All objects that emit events are an instance of the ‘EventEmitter’ class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to the named events emitted by the object. Typically these event names are camel-cased strings but any valid javascript property key can be used.(reference: nodejs.org)

The theory part can be found on the link provided here.

I will try to explain all this in my way . Lets take an example.

Basic Example of event emitter.

In the above example, I have tried to show how and event emitter works. First, we used the required variable to call/import the events module in node.js, and now to work with the EventEmitter class, we create an object and store it in eventEmitter variable. As mentioned above All objects that emits events are instance of “EventEmitter” class. So now after creating the object we use the eventEmitter.on() function that as written above , allows one or more function to be attached to the named event emitted by the object. The named event in the above example is tutorial1.

The code is only going to execute when the ‘tutorial1' event as written is going to occur. To make the event emit we use eventEmitter.emit() function to make a particular event occur. Thus the event occurs and the function written is called thus giving us the output “Tutorial1 event has occurred.”

proof of the above sample questions output.

This was a basic example to explain the events module.

Example with parameters

Now you should be thinking what is the use of a function in that eventEmitter.on() function. It is helpful in working with parameters for example in the above code we took an example of taking out the sum when the ‘tutorial1' event occurs. We gave parameters as(1 and 2) and the sum we got was 3 so when the tutorial1 event occurred these were passed immediately to the function and sum was returned. We can play with this taking more examples as taking classes extending this EventEmitter class and we can do more stuff with it.

example by making a class extending the EventEmitter class

So in the above code I created a class named Person which extends the EventEmitter class and then added a constructor to it with name as its parameter so when an object was made, ‘Amitesh’ was called by the constructor as name parameter and thus we again used Amitesh.on() function(generally objectname.on()) to print whatever i wanted. Thus when this ‘name’ event is emitted by this object then only that function works and call for Amitesh.name where my name is stored (using this._name=name).

I know I cannot be perfect but at least I expressed all I learned on day2 and will try to improve this in the upcoming blogs.

Thanks for reading my article.

I am Amitesh Kumar, a Btech student. You can contact me through my email id for more queries

Here is my email id: belikeamitesh@gmail.com

--

--

Amitesh Kumar
Web For You

A web developer to be…sharing whatever I learn.