Basics of Node.js

Node.js is a runtime environment for executing JavaScript code based on Google’s JavaScript Engine, V8

Nicky Liu
Future Vision
4 min readAug 14, 2019

--

Node.js is basically like going to your console inside of a web browser. It is good for building APIs, and is one of the few ways to create a backend using JavaScript.

One of the main benefits is that Node.js is asynchronous, meaning that when fetching from an API rather than waiting until they get the result before the next step, Node will proceed to next next step while waiting. Now lets jump into some basics!

Download node here: https://nodejs.org/en/

Enter some random code:

Now run the server

Now you will see the result of the console.log on your terminal!

Create a new module

Fill it in.

Export info from a module by doing

Now get the module that was previously exported in the App.js file.

This gives you whatever was the exported object.

You can set it equal to something

Now call the imported function

Run

Now the new function should be called.

If you wanted to export just the function instead of an object with multiple things attached, you can just do

Now you can call the imported function with just

When you do the requires, if it is a built in module node will just get that. If it is not a built in module or has . or / in front then it will look through your folder for the module.

Add the following to your file

EventEmitter is in caps because it is a class, not a function.

This gives us access to the event class, which contains a bunch of functions.

An event is a signal that something has changed.

Create an instance of the EventEmitter class

We can now raise an event.

This does nothing without a listener, which is a function that does something once an event is raised.

addListener and on are the same thing. They take two arguments, the event that it is listening for, and the function it is to run upon detecting that event.

Event arguments are arguments that are data that are sent when an event is raised. Typically sent as an object.

To have the listener make use of this information:

If you run it again it will print out the object from emit.

You can also use an arrow function:

But the event that raises the notification should be in messenger, together with the message function, so move that over to messenger.js. Don’t forget to also require and make an instance of the EventEmitter class.

But if you run it now the event listener won’t notify you of the event. This is because both modules have a different instance of the EventEmitter class.

So rather than working with the EventEmitter directly, you create a class that acts similarly. We will use the Messenger class for this. Don’t forget to remove the word function before your functions now, as classes don’t need it. Also change the export to export the class.

Adding “extends EventEmitter” to a class gives it all of the functionality of the EventEmitter class. You then no longer need to make an instance for Logger to work with as well.

Now back in app.js you no longer need the EventEmitter instance as well, since you are working with the one from Messenger. You instead use an instance of that.

So we can now create a server, and we have our code set up nicely by breaking it down into several modules. Now let’s move on to one of its biggest draws: being used as an API.

The HTTP Module is used for creating backend apis.

This server is also an EventEmitter. Every time there is a new connection or request the server raises an event.

Now if you go to

Your terminal will print out “New Connection!”

We can change it so that instead of console.logging, upon receiving a connection there is a response based on the connection.

If you stop and restart your server on your terminal, you will see that now the page says “Hello World”. It displays this because we connected with just localhost3000. But if you instead connect with localhost3000/api/heroes now you will instead see an array of objects that are in JSON form!

Instead of listening only on port 3000, set it to find the assigned port and listen there.

We have learned how to use Node.js to run code, and how to use it to set up a site that functions like a basic API. Stay tuned for my upcoming article on using Node.js, Express, and MongoDB to create an API with CRUD (create, read, update, delete) functionality!

--

--