Intro to Node.js

2400х1260-rw-blog-node-js.png

According to node.js’s official documentation,

Node.js is a JavaScript runtime built on chrome’s v8 Javascript engine. node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js package ecosystem “npm” is the largest ecosystem of open source libraries in the world.

Before we get into understanding the complex definition of node.js, let’s understand why node js is popular in the dev community and amongst top companies like Microsoft, Netflix, Uber, Linkedin and there are so many.

Node.js was initially written in 2009 by Ryan Dahl, and during that time apache server was so popular, but the apache server was not able to handle large amounts of concurrent connections (c10k problem).

giphy.gif

Node.js was created to solve the problem of handling as many concurrent connections and at the same time not losing the performance of the server, node.js solves this problem because it works on a single-threaded event loop model. And node.js is cross-platform, which means node.js apps can run on Windows, Unix-based systems, macOS.

Now let’s try to understand some of the highlighted terms above so that we can understand node.js properly.

  • node.js is a way to run JavaScript in the backend as a runtime in a server-side environment, to create server-side apps. I.e, apps that can run in the server, which has access to the file system of an Os of that server.
  • Node.js does these awesome things with the help of chrome’s v8 JavaScript engine.

JavaScript Engine

A JavaScript engine is a computer program, which converts JavaScript code into machine code (in the form of 0’s and 1’s).

chrome’s v8 JavaScript engine

  • v8 is one of the popular JavaScript engines, developed by Google for its Chrome browser, and v8 is an open-source project as well. Every browser has its own Js engine like Apple has Nitro, Firefox uses spider monkey.

NOTE:- Node.js is not a programming language.

Runtime

A runtime is a tool that provides custom functionalities, prewritten libraries, functions, objects specific to an environment. Like in Chrome it allows JavaScript developers to do things like manipulating dom (document object module) via v8.

And in the same way in the case of node.js, it provides functionality, functions objects to do things setting up the server, reading and writing into disk/dB through file system via v8.

image3.png

Event-driven

Event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user’s actions like mouse clicks, key presses and there are several other events as well.

Non-blocking I/O Mode

Non-blocking I/O: I/O stands for Input/output and non-blocking means, the code is asynchronous, such that our other piece of code can execute while a particular piece of code block completes its execution, which makes a program more efficient as compared to the synchronous code block.

Blocking code example

image2.png

Non -blocking code example image1.png

Conclusion:

giphy (1).gif

In this article we have covered a very basic intro to node.js, why node.js was developed, how this solves the c10k problem (inability of a server to handle 10,000 concurrent connections at the same time), why node.js is popular amongst developers all across the globe, we learned about javascript engines, runtime, and non-blocking I/O mode. In the coming articles, we will learn more about node.js, the rest APIs, CLI applications that can be built using node.js.