What is Node.js?
Node.js official documentation website explains: “Node.js is a JavaScript runtime built on the V8 JavaScript engine”. From this definition, more questions may come to mind:
- What is a “Javascript runtime”?
- What is a “Javascript Engine”?
- What is V8?
In order to fully understand what Node.js is, you have to know what these terms mean. I’ll explain them to you in simple terms.
Table of Contents
What is a runtime environment?
A runtime environment is the environment in which a program is executed.
For example, as human beings, we need an environment, surroundings, or conditions that allow us to keep alive. Similarly, a software program needs an environment, hardware infrastructure, and software that supports the real-time functioning of the code.
“Runtime environment” is often abbreviated as RTE.
What is a JavaScript engine?
Virtually every Web browser has a JavaScript engine. This engine is a program that takes the JavaScript code and converts it into code the computer can understand (machine code).
For example, Chrome uses a JavaScript engine called V8. Firefox uses an engine called SpiderMonkey. The Web browser provides the runtime environment for the JavaScript code.
Node.js JavaScript Engine
Similar to a Web browser, Node.js is an execution environment for JavaScript. In addition, Node.js provides modules that allow us to do things that JavaScript cannot do within a browser.
For example, using Node.js we can
Basically, Node.js is a program that includes the V8 JavaScript engine and additional modules that are not available in the Web browser. Chrome and Node.js share the same JavaScript engine but provide a different execution environment.
Why was Node.js created?
To understand why Node.js was created, we have to think back to how JavaScript was used several years ago. Usually, JavaScript could only be used to run applications inside a Web browser.
It was until 2009, that the only way to execute JavaScript code was within the browser. That year, Ryan Dahl, had an idea: “What if you could execute JavaScript code not only in the browser, but outside of it?”.
So Ryan took the JavaScript engine V8, placed it inside a C++ program, and called that program Node.js
Important features of Node.js
Two notable features of Node.js are the following:
- is oriented to asynchronous events and
- is designed to build scalable networked applications
What is asynchronous?
In this context, asynchronous means that the code allows the program to request a task, and without waiting for this first task to complete, it can request another task. The idea is not to block the thread of execution.
Imagine that you go to a restaurant, and the waiter approaches your table, takes your order, and delivers it to the kitchen. While the chef prepares your dish, the waiter takes another table’s order. The waiter can take orders from more than one table. You don’t have to wait for the chef to finish preparing a dish before taking the next table’s order.
Node.js apps work in a similar way. The restaurant waiter is like an execution thread that is assigned to handle requests. A single thread of execution can handle multiple requests.
For example, suppose a thread in Node.js is asked to query the database. The thread of execution sends the request to the database, while the database is executing the query, that thread does not wait for the database to provide the result but is used to make another request.
What kind of projects should Node.js be used for?
Node.js is primarily used for non-blocking and event-driven applications and servers. Node’s architecture makes it ideal for programming real-time applications, such as a chat application, or for developing Web application back-ends and APIs.