What is Node.js and REPL

What is Node.js and REPL

What is Node.js and REPL

What is Node.js ?

In a simple language, Node.js is a JavaScript cross-platform and open source JavaScript runtime environment. Node.js allows to be run the JavaScript code on the server. Node.js runtime built on Chrome's V8 JavaScript engine. Node.js can be runs on various platforms (Windows, Linux, Unix, Mac, etc.).

Node.js eliminates the waiting, and simply continues with the next request. It runs single-threaded, non-blocking and asynchronous programming.

What is V8 JavaScript Engine ?

All programmer knows computer only understand machine language (binary language). It cannot understand any other language like- English, Japanese or other. When we are writing a program, a backend program (compiler) convert it into machine language. after that computer understand it and give response it also in machine language. Here compiler also convert it into English language and send to client.

As such, V8 engine is Google Chrome's backend engine that provide to convert JavaScript code into machine language and also its response into client understandable language. There are more engine available for different browser also such as V8 Engine for Google Chrome, SpiderMonkey for Firefox and Nitro for Safari browser, etc.

History of Node.js

A Google Engineer Ryan Dahl has an idea came into his mind in 2009, that how to run JavaScript code outside browser. He was very excited when his ideas was successfully implemented. He initially written in C++ language with V8 engine and called it Node.exe later on become Node.js.

Synchronous Programming vs. Asynchronous Programming

In Synchronous Programming execute one code/request at a time and another code/request are in queue for waiting till first request is not finished. It means Synchronous means you can only one code execute at a time.

In Asynchronous Programming execute one or multiple code / request at the same time and more upcoming request go for execute at the same time. One by one responses will be sent to client, not according to its upcoming queue but its execution taking time. It may be the last request get the response first. So it is called non-blocking system.

Node.js process state

It may be understand by an example in Restaurant with two ways :

1. When a customer come and order to waiter for Roti and Matar Paneer. Waiter take the order and go to the kitchen and tell to cook the order and wait for ready the order. Whereas another customer come and wait for waiter to give an order. After sometimes waiter come with Roti and Paneer serve to first customer. After that it take order from second customer .... and so on with all the customer. This is Synchronous Processing.

2. When a customer come and order to waiter for Roti and Matar Paneer. Waiter take the order and go to the kitchen and tell to cook the order for ready. After that waiter take second, third and other order from other customer. He is not waiting for ready the first order. Waiter give the second, third and all order to cook also. Waiter serve the order one by one to customer, which is ready by cook first at the same time. This is Asynchronous Processing.

Why Use Node.js ?

Node.js is fast, lightweight, efficient and an open source, server-side script which run on the Google V8 engine. It support asynchronous mode of operation, event driven I/O rather than using the traditional threads for each process.

Only one language need, JavaScript platform and full stack web development system.

  1. Easy-to-learn : Node.js is based on JavaScript language, and this language is one of the most popular for front-end web development. Therefore Node.js is easy for developers to switch on it, and most important thing is now JavaScript is also used for back-end as-well-as front-end, developers don't need to choose any other back-end languages.

  2. Cross-platform compatibility: Node.js is very easily adjustable with various platforms like- Windows, Linux, Mac OS X, Unix, and mobile platforms.

  3. Asynchronous / Non-blocking single thread: Each task in Node.js requests stack are simultaneously executed. Node.js does it all in a single thread. Each API of Node.js library is non-blocking.

  4. Event-driven Programming : In programming context, events are any action made by a user, such as click, double click or any other key action by user. But here in Node.js event means are also same but a bit feel like changed, A server built with Node.js uses a notification mechanism called “Events” to receive and track responses of previous API requests.

When Use Node.js ?

Node.js is a versatile JavaScript runtime environment built upon event-driven programming that enables non-blocking I/O (Input/Output) capable of serving multiple concurrent events in a single thread. This makes Node.js fast, lightweight, scalable, and efficient in handling data-heavy and I/O-heavy workloads characteristic of several types of web applications.

  1. Complex Single page applications (SPAs) : Now a days Single Page Applications (SPAs) are the more popular in web development market. Which an entire applications are built in a single page. Node.js is a good fit for building social networking platforms, dynamic websites, and mailing solutions.

  2. Real-Time Chats : As we know Node.js offers advanced functionalities like Asynchronous / Non-blocking and all requests manages in single threaded, made it faster processing communication. So real-time chatting make easy and faster than used with other technology. Resultant, during chatting live transmission of text, video, or audio messages from a sender to a receiver are fastly very easier .

  3. Data Streaming Applications : Node.js is excellent for the development of streaming applications thanks to its native Stream API. It has an interface of readable and writable streams that can be processed and monitored very efficiently.

    Giants like Netflix welcomed Node.js to serve 190 countries and 100+ million hours of streaming across 120 million users and counting. Node.js has a definite edge on this as the processing files at a super-fast speed and feather-light encodes and uploads.

  4. IoT (Internet of Thing) application development : In the recent decade, web application are more popular over the internet. Internet is a network of devices such as sensors, beacons, actuators, and any other items to send and exchange data. IoT may consist of thousands of such devices, which makes it challenging to manage requests and data streams from and between the devices.

    Node.js event-driven server architecture and asynchronous processing relevant heavy I/O operations on IoT networks. Furthermore, Node.js operates on writable and readable channels and streams, which is why it is the most suitable platform for IoT app development.

How to install, configure and execute a sample program in Node.js

NPM (Node Package Manager)

Node Package Manager is also called as "npm", which is the default package manager of Node.js. When we are installing Node.js, npm has been already installed. In the Node.js library it contains many useful modules. After that, more modules are downloading as our needs.

Now you can also check "npm" like as "node" at command prompt (CLI)

 npm -v
execute command for check npm

What is REPL?

Just like window provide a command line interface (CLI), in CLI (c:\users\Bhagwan Babu>_ ), Ubuntu provide terminal, in terminal Bash shell (user@ubuntu:~$) for execute some command for run some operation, like create, read, print, etc.

Node.js also provide and command line environment called it REPL (Read Eval Print Loop). REPLs provide an interactive environment to explore tools available in specific environments or programming languages.

How to run REPL ?

After installation of Node.js, when you write code "node" and execute it, a new prompt show (>), here all REPLs command will be executed.

execute command for REPL environment

After that if you want to know REPL supported command that are run on REPL prompt (>). Type on > .help

execute command for REPL environment

.help command displays some command for run on REPL (>) environment.

Here you can create JavaScript file through .editor command, .exit command is used for exit from REPL environment.

In this article, we are studying about Node.js, when we use Node.js? why use Node.js? What is v8 engine? Synchronous vs Asynchronous programming? What is NPM? What is REPL? What is the way to run REPL.

You can also read about what is MERN?

In the further article, we are studying more about programming in MERN stack.

0 comment(s)

Leave a Comment