We’ve started leaning about Node.js in class and… it’s not easy! After reading many articles and watching many videos, I think I understand a bit more… So I’ll try to share what I’ve learnt here.
What is Node.js??
Node.js was originally written by Ryan Dahl in 2009, who wasn’t happy with the limited possibilities of the most popular web server back then (Apache HTTP Server) to handle a lot of concurrent connections (up to 10,000 and more) and the most common way of creating code (sequential programming), when code either blocked the entire process or implied multiple execution stacks in the case of simultaneous connections.
I found that the analogy of going to the doctor helped to understand this problem better.
Traditional thread-based system
When you go to the doctor’s office, you go to the reception and have to fill in forms. You might do so standing at the desk, taking as much time as you need, while a queue is forming behind you. If you’re doing this, the receptionist cannot help any other patients, as you’re blocking her. Of course, there could be more receptionists to make the process go faster, but it would cost money, and it would take more space, needing more desks.
It is the same thing for web applications. Traditionally, they were written in a client/server model where the client would demand resources from the server and the server would respond with those resources. The server only responded when the client requested and would close the connection after each response. It would then be free to take on other requests, but for that, it would need to make the others wait.
In order to avoid too long a wait, threads came along, allowing systems to run multiple operations concurrently. So every request would open a new thread, every thread had all it required to run the same code to completion. But in the same way as employing more receptionists would cost too much money and space, too many requests, creating too many threads, would end up consuming a lot of memory and system resources.
The event-based system
Once again, you are in the doctor’s office, and the receptionist hands you the forms. But this time, instead of filling them standing at the desk, you go sit and let her deal with other patients (you are not blocking her) until you’re done, at which point you go back in line to hand her your forms. If there are additional forms to fill in, or if you’ve done something wrong, the receptionist will tell you so, you will go back to your seat, update the info, and go back in line once you’re done.
Once the doctor is ready to see you, the receptionist will call you to let you know. The event of the doctor being ready causes the receptionist to perform some function/action (which we call a “callback function”).
Same thing with Node and its use of the event loop. The event loop is basically a program that waits for events and dispatches them when they happens (as shown on the diagram below). It employs a single thread, with one process at a time. So instead of requiring a new thread or process for every single request, Node takes all requests and then delegates most of the work to other system workers.
When the background workers are done doing their work, they emit events to Node callbacks registered on that event.
Callbacks are essentially functions passed into other functions as arguments and are called when certain conditions occur.
So what Node developers basically do is write event handlers that get called when certain Node events happen.
Here’s a great video about the event loop:
This is a shift away from the traditional models we just discussed, that run larger, more complex processes and run several threads concurrently, with each thread waiting for its appropriate response before moving on.
Another great thing about it is that, like the second example of our doctor’s office, the callback function handles requests without blocking input/output (I/O) calls.
Downloading and using Node.js
It is available to download from https://nodejs.org/en/.
npm (Node Package Manager) is used to manage Node’s dependencies and to share and borrow packages all over the world.
In your terminal, type
npm install to install all the packages listed in the
package.json of the node project you’re working on (if there is one).
All modules listed as dependencies in
package.json will be installed in a local folder called
npm init can be used to set up a new or existing npm package.
server.js file in the root folder and enter the following code:
In the command prompt, type in
node server.js and hit enter. You’ll see something similar to this.
node server.js //server listening on port 3000
Open your browser and hit localhost:3000. You should see a
Hello Node.js servermessage.
There are many more npm commands that you can check out on this link.
This short video also explains what npm is quite well:
If you’d like to read other articles about Node.js, check out those ones: