DEV Community

Cover image for Understanding node.js before diving in
Santiago Rincón
Santiago Rincón

Posted on

Understanding node.js before diving in

There are a lot of articles on the Internet that explain what node.js is, nonetheless, for those of us without a computer science degree it all might sound really complex and confusing.

The approach of this post will be trying to explain conceptually and in context, what node.js is, how it works, and why to use it, in a way that anybody can understand. Then I'll say how you can say "Hello" to the world using node.js.

If you want to go straight to the action, go to the Getting Started section.

What is Node.js

Let me make things clear here before we start conceptualizing, it might not be your case but believe me, I have seen people comparing node.js to things such as Apache or Tomcat.

Please, guys, just don't... Those are products ready to install servers and easily allow you to deploy your app. Sure, you can add a module for PHP and a module to allow SSL connection with node.js, however, you will need to add some code to get your server running. We will talk about modules and servers later.

I have also seen people comparing node.js to Django and once again, no, Django is a framework for back-end development built with python, a tool to ease the development of web apps. In node.js we have Express for that——Though they are built with different approaches——and again, we will talk about it later.

Now that we made things clear, we can continue.

The node.js official website defines it as an asynchronous event-driven javaScript runtime, designed to build scalable network applications.——Awesome, isn't it?

Good Mythical Morning What GIF By Rhett And Link<br>

Yeah, I know, the first time I read these words I was like–—Yeah! sure, piece of cake, I know PHP and some Python, what can go wrong?——It all went wrong, I didn't have a clue about what I was facing, and that meant a lot of problems then.

Let's dig inside node.js concepts while we see how it works, and hopefully, that will help you understand everything I'm talking about once you finish reading this post.

So, is it javascript or what?

Let's see, node.js was born when developers of javascript extended it from something you could only run in browsers, to something you could run on a machine.

That allowed us to build with javascript more than just interactive websites, providing us the capability to do stuff that other scripting languages like python can do, like back-end, for example. :)

Now, Did you know that the only language computers understand is binary? Yes, the only thing they understand is a sequence of 0 and 1. Those languages closest to that thing we know as "machine architecture" are low-level languages and those closest to us (programmers) are high-level languages.

Both node.js and javascript are high-level, and they run on the V8 javascript runtime engine. This engine takes your high-level code and converts it into something your machine can understand.

By the way, to make it possible, it requires time to execute a lot of default functions that ease our work and that is what makes it a runtime. There are a lot of instructions running while a program is being executed——It's deeper than that, but that's enough for now.

Non-blocking asynchronous

Ok, I think this is one of the most confusing things about node.js. I have seen a lot of people misunderstanding these concepts but it is actually really easy once you get it.

If you think about synchronized swimming, you may imagine a group of people making the same movement in parallel in a swimming pool. So, synchronous refers to it, operations taking place at the same time.

Asynchronous is literally the opposite of synchronous. Now, think about a restaurant. There are waitpersons who have to take the menu to the customers, to serve them food, to give them the bill, to clear the table once they are gone, etc.

These are asynchronous operations and customers' requirements (menu, food, bill, etc) are blocking calls. But you will never see a waitperson waiting to finish with one customer to start with another. Instead, they attempt to each table simulating some kind of parallelism which improves performance.

This kind of parallelism is what we call non-blocking architecture. Let's see what happens when we code.

console.log('Hello, World')
console.log('I'm learning Node.js')
Enter fullscreen mode Exit fullscreen mode

The result will be as expected, nothing unusual. First, you will see Hello, World, then, I'm learning Node.js but... what if that "Hello, World" comes from an operation that needs time to finish, something like a blocking call?




I will assume you know a little bit about the EcmaScript standard and arrow functions. If not, please, go and read.


Let's simulate it.

setTimeout(() => {
  console.log('Hello, World')
}, 5000)

console.log('I'm learning Node.js')
Enter fullscreen mode Exit fullscreen mode

Result

I'm learning Node.js
Hello, World
Enter fullscreen mode Exit fullscreen mode

What happened here? As we are using an asynchronous operation, this kind of approach will not work as expected. Sure, the first event will be blocked because it needs time to finish the operation but it will not put your threat to sleep.

I don't get it, why are we seeing first the next operation then? Is it executing two operations at the same time? Isn't it synchronous? No, it is because of the non-blocking architecture of node.js.

Let's see. In other languages, tasks are executed one by one, node.js can execute several asynchronous tasks at the same time which means that it will return immediately with whatever result it has. And how is it possible?

I don't know if you noticed. but we are passing around a function to setTimeout() as we do with variables in other languages. It's a callback function and it, my friends, is the key to make asynchronous possible in node.js.

In this context, a callback function defines what will be executed after the blocking call finishes. In fact, all of the asynchronous patterns in node.js are based in callbacks, they just add syntactic sugar to ease our work.

Now, setTimeout() is an async function that schedules a callback execution once a given time has passed. In simple words, it delays a function execution but other stuff can be happening at the same time outside this function thanks to the non-blocking architecture of node.js.

Maybe you are thinking "What if I configure the delay to 0 seconds"? Well, let's see what happens.

setTimeout(() => {
  console.log('I'm learning NodeJs')
}, 0)

console.log('Surprise')
Enter fullscreen mode Exit fullscreen mode

Result

Surprise
I'm learning NodeJs
Enter fullscreen mode Exit fullscreen mode

Wait, what? Yes, configuring the delay to 0 seconds will not mean that it will be executed first because the callback is added to the event-loop so it has to wait for its turn before being executed.

Did you say event-loop? Yes, it is responsible for scheduling the asynchronous operations, it is the heart of node.js, and this takes us to the event-driven concept which is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. This means that applications act on events.

Asynchronous is a way more extensive topic——material for another post——but we have the basics we need to continue. If there is something you didn't understand, let me know in the comments.

Why would I use Node.js anyway

If you are a self-taught person, you should know how hard it can be to choose the perfect technology for your project. Node.js is not always the right answer but it could be for some cases, so first, let's try to figure out if you need to use node.js by understanding the problems it solves.

A few years ago I developed this prototype for a social network using PHP. It had a mixed back and front-end so it was a mess but it actually worked, at least as a prototype.

The main problem we faced at that time was the server connections. When we had over 100 active users it started to collapse, we needed a better server but we had no money for that. That situation among other management issues, made the project fail.

On the other hand, this app used third party API's, and a lot of other features that made it slow.

A few months later I had started a project lo learn customizing Bootstrap and there was where I met node.js. I had a javascript background so it was easy to dive in.

While I was learning, I discovered it had could help me to solve the previous project problems——Actually, even now I learn new things with node.js that would be helpful to that project.

The first important thing I discovered was that I could use javascript not just for the front-end but for the back-end.——I'm the kind of person that hates having mixed code.—— On the other hand The main goal of node.js is to ease scalable network applications development, and this is possible because of the way it works.

Single-threaded, event-driven architecture

When you are using languages like PHP, each connection creates a new thread which comes with 2 MB of RAM assigned. Depending on the RAM your system has, anyway, it gives you a theoretical maximum of connections.

As your project users grow you will need to add more servers to keep it up with the demand. This means adding up more money to cover the server costs. Besides this, we already have costs for traffic, employees, potential technical issues and more.

For this reason, the bottleneck of this kind of architecture is the maximum amount of connections that a server can handle. Node.js solves this problem by changing the way in which server connections are made.

Instead of creating a new thread per connection, each one triggers an execution event inside the process of the node.js engine, which means that node.js operates only with one threat without having a blocking call to start the event-loop as similar event-driven systems do.

All in all, node.js allows your application to hold tens of thousands of simultaneous connections inside the event-loop. But... what happens when the application is so big that becomes extremely difficult to maintain it?

The rise of the Microservices Pattern

Apps start out small, then, they start growing and you can use the MVP pattern. It might ease your firsts maintenance problems, for sure. But it could grow bigger, more features demand and more new users with a lot of expectation.

Without even regarding, you may end up with a project so big that not even your huge and experienced development team could cope with no struggling.

It grows more and more and becomes more complex until you realize that just thinking about adding a new feature is a complete nightmare.

If you are just developing websites you might think this never happens but it's a common problem for companies and its answer is the microservices pattern.

A microservice is a single self-contained unit which, together with many others, makes up a large application. By splitting your app into small units. Every part of it is independently deployable and scalable, can be written by different teams and in different programming languages and can be tested individually.——Max Stoiber.

That means that you can have different teams working in different features and you will not need to deploy the whole code every time you add a new feature.——Something really useful if you ask me.

npm

Last but not least, node.js provide us npm (Node Package Manager) which allows us to install, update and use smaller open-source package (Those modules we talked about at the beginning of this post) into our application, so you will not need to write everything from scratch.

I have heard people concerned about privacy and security when it comes to using node.js. It's understandable, government, Fintech or MedTech organizations may wary of storing their data in the cloud. For that, npm released the Enterprise version which allows you to run npm's infrastructure behind your company's firewall.

It gives businesses access to a private registry with advanced security features to help them control access to code, identify vulnerabilities, and automatically replace unsafe code.

Getting started

Now that hopefully, you understand what node.js is, let's give the next step.

Install

To install node.js you need to download it first from its official website.Download Node.js

Follow the instructions to install it.

When finished, you will end up with both node.js and npm.

Let's verify if everything is ok. Open the command line and execute the following.

node -v
Enter fullscreen mode Exit fullscreen mode

It should return something like this but with the version you just installed

v12.16.0
Enter fullscreen mode Exit fullscreen mode

Now, verify npm

npm -v
Enter fullscreen mode Exit fullscreen mode

Again, you will get something like this but with the installed version

6.13.4
Enter fullscreen mode Exit fullscreen mode

Hello, World! with Node.js

I think this is one of the easiest "Hello, World!" we will do, ever. Create a new file.

touch hello.js

// If you are using cmd on windows use the following
type nul > hello.js
Enter fullscreen mode Exit fullscreen mode

Open it with your favorite text editor and add the following line.

console.log("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

Now just execute it using the node command.

node hello.js
// Output: Hello, World!
Enter fullscreen mode Exit fullscreen mode

And that's it. A bunch theory, just one line of code and you already got started with node.js. :)

Please give this post some love and share it if you found it useful. If you think there is something I'm missing, please let me know in the comments below.

Good Mythical Morning What GIF By Rhett And Link<br>

PS. Take your time to digest all this theory because I'll be back with some interesting practice soon.

Top comments (2)

Collapse
 
davidgala306 profile image
David Gala

Very interesting!
I look forward to more of your posts!

Collapse
 
thehomelessdev profile image
Santiago Rincón

Thanks, bro. I'm planning something interesting. :)