What is Node JS??? Building Blocks & Architecture???

Minhazul Hasan Sohan
3 min readDec 22, 2020

--

Node.JS

Node JS is an open-source, cross-platform JavaScript runtime powered by a Chrome V8 engine that executes JavaScript code outside a web browser.

What is the runtime environment really??? Well, before node we use JavaScript only to build applications that run inside the browser. Every browser has a JavaScript engine that takes our JavaScript code and converts it to code that a computer can understand. For example, Microsoft Edge uses Chakra, Firefox uses Spider Monkey, Google Chrome uses V8. Because of these varieties of engines that sometimes JavaScript code can behave differently in one browser or another. The browser provides a runtime environment for JavaScript code. For example, the window or the document object allows us to work with the environment in which our code is running.

Up to 2009 the only way to execute JavaScript code was inside of a browser. In 2009 a creative programmer Ryan Dahl came up with a brilliant idea. He thought it would be great to execute JavaScript outside of a browser. He took Google’s V8 engine which is the fastest JavaScript engine and embedded it inside a C++ program and called that program Node.JS. So, similar to a browser Node is a runtime environment for JavaScript code that contains a JavaScript engine that can execute JavaScript code. Also, it has certain objects that provide an environment for our JavaScript code but these objects are different from the environment objects we have in browsers. For example, we don’t have the document object instead we have other objects that give us more interesting capabilities, for example, we can work with the file system, listen for requests and a given port, and so on.

So in essence Node is a program that includes the V8 JavaScript engine plus some additional modules that give us capabilities not available inside the browser. Both Node and CHrome share the same JavaScript engine but they provide different runtime environments for JavaScript.

How Node works???

Well at first we have to know how the webserver works? Web server contains backend web application which can access by a client. That client could be a browser or a mobile application, etc. When a client is a web browser, it will expect HTML or data from the server but if the client is a mobile app, it will expect data from the server.

Now there have two kinds of request:

  • CPU intensive task (done by server CPU)
  • I/O intensive task (request to another server, fetch data from the database, read data from a file system by the main server)

We know that JavaScript is a single-threaded language. So Node JS works with a single thread. Here comes the main two concepts of Node JS.

  1. Non-Blocking I/O
  2. Asynchronous (Call-Back)

Non-Blocking I/O means the single thread sends the request to other workers by accepting the request and not to block for the request. These workers are implemented by another concept known as libuv, which is a special type of library specially made for Node JS by C Language. Libuv gives us the Non-Blocking I/O concepts. So the thread can accept multiple requests without being blocked. But this mechanism works only for I/O intensive tasks. It’s not suitable for CPU intensive task because of the single thread. We work with Node JS for I/O intensive tasks when we want to interact with external systems like databases, file-system, or network.

Now, what will happen when the response is back after accepting the request. Therefore we have another concept for Asynchronous or Call-Back. After finishing the task it returns an event. That’s why Node JS has also known as event-based architecture.

There has another concept to talks about Node JS called Event Loop. It can assume a simple infinite while loop that waits continuously for a new task and delivers the task to the exact location one by one.

--

--

Minhazul Hasan Sohan

চিত্ত যেথা ভয়শুন্য, উচ্চ যেথা শির