What is NodeJS?
Tags: node, javascript
Published: 2020-03-26 01:56:15

By definition, Node.js is an open-source cross-platform server-side runtime environment built on Chrome’s V8 JavaScript engine, which executes JavaScript code outside of a web browser. But what exactly does all of this mean?

Put more simply, Node is a framework that allows us to run JavaScript on the server. Let’s take a closer look and flush out exactly what Node is and why you should understand it.

#### Background
Each browser has its own JavaScript engine, which functions to translate JavaScript code into code the computer can read. *(As a quick aside, this is why JavaScript may behave differently in different browsers.)* Chrome’s engine is called V8 and is the fastest JavaScript engine available. 

Before the creation of Node, JavaScript could only be run in the browser. This is because the browser provides certain objects that assist with executing JavaScript tasks. In order to run JavaScript outside of a browser, we would need objects that could allow us to manipulate files, databases, and servers. This is exactly what Node was created to do. While other tools and frameworks (ASP.NET, Django, Rails) tackle this same problem, Node has one massive benefit - it can act asynchronously. 

#### JavaScript Event Loop & I/O
JavaScript’s event loop consists of a call stack. It works through the call stack, executing each request in order. The stack works efficiently to save time and enhance performance. If a request requires a database query, its corresponding callback is sent to a secondary queue so that the main request can continue to execute. It doesn’t wait around - **it continues working through the call stack and will come back to the initial request once it has everything needed to execute it.** So, once the query is returned, that callback is dropped into a waiting queue so that as soon as the engine is free to execute it, it will run.

Event looping is how IO, or input/output, is managed. IO consists of everything from reading and writing files to handling HTTP requests. This can be managed either synchronously (blocking IO) or, as with Node, asynchronously (non-blocking IO). 

Blocking IO will literally stop everything from executing until the initial request is completed. During that time, memory and processing are being consumed while performance dwindles. Alternatively, non-blocking IO benefits from the asynchronous event looping of JavaScript. The server can serve many requests at the same time in a more efficient and performance-rich fashion. 

#### Node Modules & NPM
Node modules are open-source libraries that are shared across the community because they solve a vast number of basic problems. Each module is independent and doesn’t impact other code within a project. This allows us to modularize our projects, adding in preexisting code to handle common needs. These modules are bundled together by way of NPM, Node package manager, implementing a number of complex solutions and features in one simple file - a significant boost in development efficiency. 

#### Why Know Node?
With just this basic understanding of how Node works, we begin to see why it is so valuable. Node enables us to generate fast, responsive, dynamic page content. This is all because Node allows us to CRUD *(create, read, update, delete)* resources on the server, modifying and returning data from the database on the fly. 

Many leading websites that you likely use regularly, from Netflix to Uber, use Node. It makes sense to use Node purely due to the powerful, user-friendly performance benefits. Add in the simple and invaluable implementation of libraries to create a rich network of features that manage everything from data to routing, and even animation - it’s easy to see why Node is an indispensable tool.