Node JS Tutorial from basics with installation, environment setup, Modules, Express JS, Web API and Mongo DB. Step by step guide to learn Node JS with live code examples.

What is Node JS?

Node JS is an open source and cross-platform JavaScript Runtime to run JavaScript outside web browser. Node JS is used to build scalable web/window/console application, Web APIs and Hybrid applications. It is build on Chrome's V8 engine.

Node JS is asynchronous and event-driven JavaScript runtime build for scalable network applications. Although JavaScript in web browser is single thread and synchronous, Node JS is asynchronous and can use multi threads for I/O or others tasks in background.

JavaScript was meant for web browser only, but using Node JS, we can use JavaScript outside web browser to build scalable applications.

Node JS Timeline

Year Event
2009 Node JS Founded by Ryan Dahl
2010 NPM Introduced
2011 Node JS released for windows
2012 Ryan Dahl left Node JS
2014 Node.JS Foundation formed
2019 JS Foundation and Node.JS merged to OpenJS Foundation.

To learn Node JS, The fundamentals of web are required. Please go through our HTML, CSS and JavaScript Tutorial for revision.

Node JS is the Top most Popular Web Technology and Framework in Stack Overflow 2022 Survey with 47.12% voting. React JS is second with 42.62% voting.


Node JS Applications

Node JS is based on Chrome's V8 engine, one of the fastest JavaScript engine. Node JS use asynchronous event-driven runtime. Node JS can handle many concurrent connection at one time.

The JavaScript Execution in Node JS is single thread and Non Blocking making Node JS one of the Fastest Backend Server that can handle many concurrent at time with low RAM and best use of CPU.

Node JS based applications

  1. Web Application Development.
  2. Hybrid Apps Development. (Both Desktop & Mobile)
  3. API Development.
  4. Chat Applications.
  5. Streaming Services for Video and Audio.
  6. Console Applications

Node JS Based Web Servers

  1. Netflix
  2. Google (Partially)
  3. Twitter
  4. Linkedin
  5. Uber
  6. Paypal
  7. Nasa
  8. Ali Express
  9. Medium
  10. Msn
  11. eBay
  12. Slack
  13. Free Charge
  14. Flipkart Seller Portal

Node JS Based Desktop Applications

  1. VS Code
  2. Brackets
  3. Atom
  4. MongoDB Compass
  5. Postman
  6. Discord
  7. Dropbox
  8. Figma
  9. Github Desktop
  10. Microsoft Teams
  11. Skype
  12. Wordpress Desktop
  13. Whatsapp for Desktop
  14. Facebook Messenger for Desktop

Node JS Performance Secret

JavaScript is Single Thread for execution of code in a web browser. For long operations like Timers, Ajax, Fetch, Promises, JavaScript use Async callback functions. The same way Node JS works, but by using asynchronous and event driven architecture. Using this technique, Node JS can handle much more connections than Apache Web Server with very low memory.

Apache can handle upto 10k connection. But Node JS can handle more then double connections at one time.

With Callback

1

3

2


console.log(1);
    
setTimeout(()=>{ console.log(2)});  // will run after 3
    
console.log(3);
    

With Promise Callback

1

3

2


console.log(1);
    
const p = Promise.resolve(2);
p.then(i=>console.log(i));  // will run after 3
    
console.log(3);
    

With Promise Callback

1

4

3

2


console.log(1);
    
setTimeout(()=>{ console.log(2)});
const p = Promise.resolve(3);
p.then(i=>console.log(i));  
    
console.log(4);
    

As we can see that there is a callback function in setTimeout function, thus JavaScript will not block here and execute third line first. After main thread is free (after printing 3), now callback function in timer will be executed by Event Loop and we will get 2.

In the same manner, file handling and database connections are handled in Node JS. By using Non Blocking approach for long waiting tasks, Node JS can handle multiple connections using a single thread.

Blocking Request in NodeJS


const fs = require('fs');
const data = fs.readFileSync('/data.txt');
console.log(data);           
console.log("done");     // will run after data      
    

Non Blocking Request in NodeJS


const fs = require('fs');
const data = fs.readFile('/data.txt',(err,output)=>{
    if(err){
        console.warn("error");
    }
    else{
        console.log(output);
    }
});       
console.log("app running");      // will run first     
    

It is recommended to use Non Blocking call for file handling in node. Using Blocking call can slow down code performance. So That's how JavaScript being single thread programming language executes very fast just because of callbacks.


Event Loop

JavaScript Event Loop
JavaScript Event Loop

As javascript is single thread, it use event loop to handle asynchronous operations. When we call a function, the call stack get occupied and will be free after function is called. Once page is loaded, call stack is free to handle events, timers, ajax, Promises and Async/await function used mainly as callbacks.

Microtask

High Priority Tasks like Promise, process.nextTick, queueMicrotask and async functions got first Priority as thet are Microtask.

Macrotask

Tasks such as Events, timers will be executes in event loop after Microtask. That is why Promises are always faster then setTimeout with 0 time.


Node JS is best for Data-Intensive or CPU Intensive Applications?

The use of single thread and asynchronous event-driven make Node JS best for Data Intensive Applications, like a Web Server where I/O is the biggest issue. Node JS servers best as a web server with very low memory and can server more requests.

Earlier Node JS was not best for CPU Intensive Applications where multi-threading is required. But Now Node JS supports Cluster and Worker modules for Multi threading and CPU Intensive Applications.


Node JS Vs PHP

Lets Compare Node JS Vs Php as a web server.

Php use Apache Web Server. Apache is Thread & Process Based server where each request is handled by a different thread (process). To assign process to different thread, Apache consumes more memory (RAM).

Node JS is based on Nginx. Nginx is a single thread web server used to handle multiple concurrent request.

Node JS Vs PHP
Features Php Node JS
Release 1995 2009
TypeProgramming LanguageJavaScript Runtime Environment
ApplicationWeb ApplicationWeb Apps, Software Apps, Console Apps
PerformanceMediumFast
ProcessSynchronousAsynchronous
Hosting SupportAll Hosting ServersShared Hosting or VPS
Learning ComplexityEasyMedium

Node JS Vs Apache Connections

nodejs vs apache connections
nodejs vs apache connections

Node JS Vs Apache Memory

nodejs vs apache memory
nodejs vs apache memory (RAM)

Node JS vs Java vs Python

As php is only meant for web based applications, we can compare Node JS with Java and Python. Actually Java and Python are direct competitors or Node JS and they can build both Web and Desktop based applications.

Properties Java Python Node JS
Type Programming Language Programming Language Runtime
Applications Backend and Software Backend and Software Frontend, Backend and Software
Coding verbose less code less code
Learning Not Easy Easy Easy
Execution Compiled Interpreted Compiled
Performance Fast Slow Fast
Active Developers 14 M 15.7 M 17.4 M (JavaScript)

For NodeJS Video Tutorials, Join our Youtube Channel.