Beginners guide to Node.js

Hey everyone! In this blog, I wanted to discuss a very popular framework known as Node.js. This blog is geared towards extreme beginners and will focus on what Node.js is, how it works, how to install, and more. With that let’s begin.

Let’s start with, what the heck even is Node.js? Node.js is a Javascript runtime that is built on Chrome’s V8 Javascript engine. Node uses javascript running on the server and is used to build fast reliable web apps. It also uses an event-driven non-blocking I/O model. This is actually what makes Node.js very fast and efficient. By working on a single thread and using the non-blocking I/O calls it can support thousands and thousands of ongoing connections.

The diagram above shows this event loop. The EventEmitter class is used to bind events and event listeners. This is all asynchronous and non-blocking. What this means is instead of waiting for one event to complete and then moving on to the next, tens of thousands of connections can occur at once.

What can we use Node.js for? The most popular use for Node is for REST APIs and backend applications. However, we can also use it for real-time technology such as chats and games, blogs, tools, and more.

How can we download Node.js? If you just go into google and type in “download Node.js” the first link will take you to the exact place where you can follow along and download.

The first step we need to do once we are in our project directory is to create a package.json file. If we run “npm init” this will ask us a couple of questions to run through to help us set up our file. The first question we get asked is the name of our application, which we can type in. The second question is the version, that is automatically set to the default and we can keep it like that. The third question is asking us for a description of our application, which we can type in a brief description of what our app does. The fourth question is the entry point, which is going to be the main file. Since we're already created app.js we can keep this as our entry point. The next three questions are test command, git repository, and keywords which we can choose to fill in as we please but are not necessary. Then, we get asked the author and we can type in our name. Lastly, the question asks us about licenses which we can go ahead and submit yes for.

After completing these steps, we would now go into our app.js file and begin writing code to start setting up our server.

With the code above, this is going to build a very simple node server on the website. Walking through the code:

In line 1 we are going to set const HTTP to require ‘HTTP’ which is a node module. This is a core module and already installed in.

In line 3 we are setting the variable ‘hostname’ which is going to be the loopback address at ‘’.

In line 4 we are setting the port to 3000.

In line 6 we are creating a variable server and are going to set that to use the HTTP module, and then set a function called ‘createServer’. Then we are going to use the arrow function notation and pass in request and response. Now that we have access to these two objects we need to set a status code.

In line 7 we are setting the status code to ‘200’ which is the code that means everything is okay. So we will set the ‘res.statusCode’ to 200.

In line 8 we want to set a header. So we set ‘res.setHeader’ to content-type, and we want that in ‘text/plain’.

In line 9 we want to get that response object so we use ‘res.end’. Here we put in ‘Hello World’. This should get the output into our browser.

In line 11 we take the server variable and add ‘.listen’, this is going to take in the port, the hostname, and the callback. Then we will console.log ‘Server started on port’ and concatenate the ‘port’ variable.

Now we want to save all that code in our terminal type in ‘node server.js’. This should respond back with ‘Started server on port 3000’. Then in our browser, if we go to localhost:3000. We should get back…

Yay! This means that the server is running.

I hope this very basic run through helped to understand Node.js a little better. The inspiration behind this blog post came from the Traversy Media Node.js Tutorial for Absolute Beginners. If you want to check out the full video I would definitely recommend it. Here is the link:

Thanks for reading :)