What is node js?


What is node js?


JavaScript is client side.

Most of us have already hear of JavaScript, popularly known as a client side scripting language, meaning the JavaScript code will be executed by the client (in majority of the cases it is your web browser).

It should be understood that with respect to a website all content related to our site or web application will reside on a server. Depending on the type of   content of the requested file will either be sent to the client directly (e.g. HTML, JavaScript, CSS, Images, pdf etc) to the client/user or if they are server side scripts, they will be first processed on the server itself and result from the same will be sent as an output to the user.

Now JavaScript is primarily known to be a client side scripting language and is actually processed by a JavaScript engine which is usually developed and is integrated in a web browser like Firefox. 

What if we could use this engine separately and process JavaScript on the server itself. That way as a programmer you will be able to use your JavaScript skills not only on client side but also on the server side.

Node.js

Node.js is a cross-platform, open-source JavaScript runtime environment that can run on Windows, Linux, Unix, macOS, and more. Node.js runs on the V8 JavaScript engine, and executes JavaScript code outside a web browser.  

Basically whatever JavaScript (server side) you will write will be executed/processed by the Node.js environment.

Node.js was initially written by Ryan Dahl in 2009.

It should be noted that  it was 13 years after the introduction of the first server-side JavaScript environment, Netscape's LiveWire Pro Web.

What can you do with Node JS

A client side JavaScript i.e. the one which runs on you web browser access the Document Object Model (DOM) which allows you to perform manipulation on the HTML page (which is loaded into the browser) via the global object "window".  

Node.js on the other hand does not have the concept of the "window" object as there is no DOM to work upon as such. In Node.js, the global object  name is  "global".  Though you need to keep in mind is that it will not provide same functionality as the "window", as we understand both serve different purpose.

With Node.js you get access to system resources like reading writing to file system, network, database connectivity etc. which are not possible in regular JavaScript.

Node.js and NPM

As in JavaScript you might have come across different external libraries, Node.js developers create a lot of packages which are used to for doing a lot of common or uncommon functionality. Theses packages or modules can be included into you node programs for a variety of work.  

For example the following code includes or call a http module which allows us to run a web server that is listening on port 8080. When called user see a "Welcome to node world"  message.

let http = require('http');

http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/html'});
 res.end('Welcome to node world');
}).listen(8080); 

Certain common modules will be available when you install the Node.js environment (https://nodejs.org/api/index.html) while other packages/modules for your project can be manages via the NPM or the Node Package Manager.

npm is the standard package manager for Node.js.  You can see the details about the same here https://nodejs.org/en/learn/getting-started/an-introduction-to-the-npm-package-manager

Node.js is asynchronous

It means that it can handle another request while the given task is being handled by the server/computer. When the earlier task is completed the results are sent to the client who requested the same.  This is in contrast to programing languages like PHP/ASP where the program will wait till the operation completes and it returns the results back.

Being asynchronous also make Node.js faster as it eliminates the waiting and it can continue with the next request.

Preferred for I/O bound applications

Input/Output based operations  or I/O bound means that the time to complete a computation is determined by the the time spent waiting for input/output operations to be completed.  In simple term if we are reading or writing to a file is an I/O bound operation as we need to wait for the read/write to complete for the result.

In Node.js the I/O bound operations are non blocking, means Node.js can server other request while the specific operation is going on.

Data streaming applications

Node.js can handle data in packets or chunks.  When we have huge amounts of data that need to be processed that we are continuously receiving, this data is usually sent in small pieces called packets or chunks. 

With  Node.js we can handle this type of data very easily and process the data in real-time.

Data Intensive Real-time Applications (DIRT)

With rise of collaborative web, mobile and HTML5 a lot of data needs to be processed in real time and need to be shared across maintaining consistency. With Node.js we can server to large number of concurrent users effectively.

JSON based API

JavaScript Object Notation is known as a native simple text based format for sending and receiving data from API.  With Node.js we can easily read and write to JSON files making it suitable for the API functionality.

Single page applications

Single page applications are webpages that do not reload or have multiple pages. All content is routed via the same page and updated according to the users need or requests in sections of the same page.  Node.js asynchronous mechanism and other concepts like AJAX makes it very suitable for SPA.

IOT - internet of things

Node.js is now not limited to just web applications but there are now hundreds of packages and modules that can be used to work with IOT applications and development boards like the Arduino.  e.g. arduino-nodejs Module for arduino-nodejs communication https://www.npmjs.com/package/arduino-nodejs.  Of course there are many other things related to IOT out there.

And more....

The post can go on and on as there is so much to node.js, but we need to stop here and look forward to practical use of node. Till next post. That it!!!

Main category
Address

OpenSourceCook.in
"Natraj"  Bungalow,
Colony No.7,  Sr.No. 38.
(Lane Behind Sai Baba Mandir)
Kale Borate Nagar, Hadapsar,
Pune - 411028.
Get Directions