Last Updated on
Node JS is the first backend platform I learned and it helped me build full-stack applications. In this article, I will talk a little about Node and the concepts that go around this runtime environment. So to answer your question in simple terms,
Looking for a Node.JS course recommendation? Check out The complete Node Developer course by Rob Percival - see others
Right out of the bat you must have figured it out what open-source and cross-platform means.
Open-source means the source code of Node JS is open to the public (like Linux) and everyone can contribute, modify and use this software without paying any license fee. So if you make an application with Node JS, you won’t have to pay any money for the core Node JS runtime.
Cross-platform means Node JS can run in all the major operating systems like Windows, Mac OS, Linux, and others so it doesn’t matter what kind of computer you own, it will still run. You can host a Node JS application in both Linux or Windows servers for deployment.
Now that the obvious is out of the way, I am going to explain the other important parts to help you understand what the Node runtime environment is and why you should care to use it. As part of the explanation, I am also going to outline other technical aspects of the runtime like the asynchronous, event-driven and non-blocking aspect of Node JS.
Table of Contents
What is the Node JS runtime environment?
Node JS has one single thread to deal with the requests and another one as the main working thread.
When a user requests something, Node JS will send that request to the thread and get back to the main worker thread. If another user does another request, it will put the next request to the same thread and get back to working again.
After some time, the request will return and Node will be notified. Node will then take the response and give it to the user.
All this process can happen in one single thread one after another and this the gist of asynchronousness in Node JS.
In other programming environments such as Python and PHP, when someone makes a request, the entire thread gets blocked. If another user needs to make a new request, the system will have to create a new thread for the operation.
Node JS works with a single thread and can get notified for fulfilled requests via events. Let me explain what it means what event-driven means in Node JS.
Since Node JS is sending the request to another thread for completion, it has to know how something returns to it. Node JS will always keep listening to various events. This is similar to how operating systems work. An operating system will listen for various events like a mouse click, keyboard key press and so on. When an event comes, the system will stop what it was doing and put its attention to the event at hand and do what is required.
Similarly, in Node JS, it can do the assign tasks and still keep listening to other events. When a user clicks or something has returned from the thread, it will do what’s needed to do. This event-driven mechanism is what makes it possible for Node to work with a single thread and be super fast.
Node JS does not sleep because it can do things one after another. It’s sort of like juggling where one item is always in the air. In Node’s case, it will juggle with one hand and keep all the other things in the air (other thread.)
If you write this command in python: sleep(5)
It’s going to literally sleep for five seconds and won’t be available to do anything within this time. This is an example of a blocking code. If you are making a server were 10,000s of users are making requests, this means for five seconds the server will not be able to serve these people.
One the other hand, this is an equivalent code in Node: setTimeout(doSomething, 5000);
Here Node will do that function after five seconds, but it will never go to sleep. This way it will still be able to handle all the other requests that come to it.
Node Package Manager, NPM
Node JS works with packages similar to how Java and Python has their own packages. NPM is a public repository where all the developers can contribute packages.
NPM comes built-in with Node JS and when you are building applications, you are going to use NPM commands to install and work with packages.
With this you will have to enter Linux like commands for installing various packages like this is how you would install the express js framework:
npm install express
- Download Node JS and start using it
- Start building something with it
- Leave a comment in this post and let me know what you think