|

What Is NodeJS?

what is nodejs

Node.js is the way to make applications using JavaScript outside of a web browser. In the past, we always thought of JavaScript as a simple toy language for silly alerts and such in web browsers. Today it is much, much more than that. With Nodejs, a whole new set of features and api’s come to the JavaScript language that previously would require a language like Python, Java, PHP, or C#. Now, it is possible to manipulate the file system, create and remove folders, query databases, and create web servers: All in JavaScript! All of this is now possible thanks to Nodejs.


Chrome V8 JavaScript Runtime

Both Nodejs and the JavaScript which executes in the browser run on the same engine. The Chrome V8 JavaScript Runtime. It is an open source engine that takes JavaScript code and compiles it down to ultra fast machine level code. Because the Chrome V8 engine is so good at this task, NodeJs is known to be super fast. The engine itself is written in C++ and it is possible to extend the engine if your ambitious enough to write some C++.


Starting a Node Process

To start a new node process, all you have to do is type node at the command prompt. We assume you have already installed node on your machine.

C:\node>node
>

This gives us the new arrow prompt which tells us that the terminal is now expecting JavaScript Node code, and not any standard terminal commands.


Your first Node application

You can now write your first Node application. Type the following in the terminal and hit enter.

> console.log('Hello from NodeJS!');
Hello from NodeJS!
undefined
>

So what happened behind the scenes? Node took our JavaScript code, compiled it down to machine code, and then printed out the result. Now again, V8 engine is running on the local machine where we installed it, but it is also running in the Chrome web browser. We can try the same thing in the console of the web browser like so.
Chrome V8 Console


Node Console vs Browser Console

So we can see that we are outputting the same result when running our tiny little program. Well, it turns out that even though each environment looks like it has a similar console, behind the scenes they are providing different tools to the developer.


The window object

In the browser, the window is the global object that stores everything you have access to. All JavaScript variables in one way or another are part of the global window object on the client side.


The global object

The Node environment has a similar concept to window, however in node it is called global. If you type global at the Node command line, you’ll see a lot of information about what the global object stores.
node global object


Exiting the Node terminal

To exit the node console, you can type the following.

> process.exit(0);

C:\node>

Now, you will be back at your machine’s command prompt and not the node runtime process. You also have the option to hit control C twice like so.

>
(To exit, press ^C again or type .exit)
>

C:\node>

Just as global in Node is similar to window in the browser, process in Node is similar to document in the browser. If you’re coming from a front end back ground those two objects are likely familiar to you. Now when learning about Node, you can kind of apply those same concepts to global and process.


What Makes NodeJS So Great?

If you visit the Node homepage, you’ll see a sentence that states the following: “Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.” Let’s talk about that for a second. Node uses non blocking IO or Input Output. What this means is that while one user is requesting a resource from a Node application, a different user can request an entirely different resource without causing any wait. Have a look at this diagram to see what we mean by blocking vs non blocking.
blocking vs non blocking

On the left hand side, we can see how a blocking application works. While we are fetching from the database, which is an IO operation, the application can not do anything else. This means our machine is wasting cycles waiting for the database to respond and can’t even do something simple like adding two numbers.

On the right, we have a non blocking scenario. When the application makes a call to fetch a user from the database, it does not have to wait for this process to finish before beginning other tasks. In fact, right after the first request is made, another different request is made to fetch a different user. In the second scenario, the application does not need to wait. The key takeaway is that in a non blocking approach, the application can run more than one operation at the same time. This leads to faster performance.

NodeJs is single threaded, or in other words, a node application runs on one single thread. This is not a problem since Node is non blocking. In a blocking context, an application can still handle more than one request, but it requires more cpu and ram resources to support building more and more threads. This is what makes Node very efficient. It uses the lease amount of resources for the most amount of performance.


NPM

npm package manager
Something else we should talk about is another sentence from the NodeJS homepage: “Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.” Node has a dedicated package manager called NPM. When they say it is the largest ecosystem in the world, they are not kidding. According to modulecounts.com, npm is showing over 600,000 packages managed by the npm system! By comparison, we can look at this chart and see that the other package managers like Maven Central, nuget, packagist, PyPI, and rubygems are lagging.
npm package counts
img credit: http://www.modulecounts.com/


What We Learned About Node

  • Node.js is a runtime environment for executing JavaScript code.
  • Node is basically a C++ program that embeds Google Chrome’s v8 engine, the fastest JavaScript engine available.
  • Developers can use Node.js to build fast and scalable networking applications. It’s a great option for building RESTful services.
  • Node.js applications are single-threaded. In other words, a single thread is used to serve all clients.
  • Applications in Node.js are asynchronous or non-blocking by default. That means when I/O operations such as accessing the file system or the network happen, the thread doesn’t wait (or block) for the result of the operation. It is released to serve other clients.
  • The asynchronous architecture makes Node.js perfect for building I/O-intensive applications.
  • Node.js is not a good choice for CPU-intensive applications, such as a video encoding service.
  • Browser environment objects such as window or the document object do not exist in Node.js. Instead, there are other objects that are not available in browsers, such as objects for working with the file system, network, operating system, and more.

What Is NodeJS Summary

So there you have it, you now know what NodeJS is. It is a JavaScript runtime that uses the V8 engine. The v8 engine is an open source JavaScript engine that is written in C++ which takes JavaScript code and compiles it down to machine code before executing it. V8 is used inside of NodeJS as well as in the Chrome web browser.

|