top

Search

Node JS Tutorial

Node.js is an open-source, cross-platform JavaScript run-time environment (Google Chrome's V8 Engine) that executes JavaScript code outside of a browser. Node.js lets developers use JavaScript to write command-line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.Official definition: Node.js is a platform built on Chrome's JavaScript runtime to easily build fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.Node.js is quite often used to build back end services like API, which power different client applications like the web app and mobile app running in mobile. Node.js helps to create highly scale scalable, data-intensive and real-time apps.Why should we use Node.js as there are a lot of other tools and framework for building back-end services like Asp.net, rails, Django etc.?Node.js is great in Prototyping and agile developments, building super fast and highly scalable services. Some of the companies that use node.js heavily are Netflix, Walmart, PayPal to name few. In fact, PayPal has rebuilt one of its application based on java and spring into node.js. And they ended up building it twice faster with fewer people, 33% fewer lines of code, 40% fewer files, 2x request/sec and 35% faster response time.Given that many companies use it, let’s understand in detail what makes it useful.Any typical web application making a service call which needs to fetch/update data in a database in the back end works like this:User does an action -> application starts processing action -> Makes a DB call -> WILL WAIT TILL REQUEST IS DONE -> Request complete -> Send back result to user.In case of a multi-thread application, the same scenario is repeated for the multi-threadRequest1 -> pick a thread from thread pool -> make DB call -> send responseRequest2 -> pick a thread from thread pool -> make DB call -> send responseRequest3 -> pick a thread from thread pool -> make DB call -> send responseAs we can see that whenever the thread is waiting for a response from DB, it spends 0% CPU. Ideally, we would like to run some other code when this happens. And in case of multi-threads, we have to allocate the memory for each thread.However, Node.js being a single-threaded application can still work with 10K concurrent requests. This is because of the event loop mechanism that it has [will learn more about it in later topics]. If we take the same example of multiple requests in node.js case, this is how it works.Request1 -> make DB requestRequest2 -> make DB requestRequest3 -> make DB requestDB request1 complete -> send responseDB request2 complete -> send responseDB request3 complete -> send responseIf we compare multithreading with node.js, both take roughly the same latency as most of the processing is on the DB side. But the advantage in the case of node.js is that we need not spawn threads, plus the cost of context switching between threads and do lots of mallocs which slows down.This may sound surprising but node.js being single-threaded is able to match with multithreading because of the event loop and node.js capability to leverage the multi-threading of database. Some of the cases where multithreading fails and node.js wins is when we need to allocate a lot of RAM per thread. And allocating lots of objects which is common in modern web frameworks, will further slowdown. This does not mean that we can use node.js in all places. Node.js is not good for intense computations/processing, which does a lot of CPU calculations. And another case is when we are having multi-core servers, we don’t use servers to full capacity as node.js is single-threaded, using only one core. Node.js is not meant for blocking operations and most of the cases of node misuses are because of this.Some application where node.js suits better are CHAT, BROKERAGE – STOCK TRADER’S DASHBOARD, MONITORING DASHBOARD (and SYSTEM),  DATA STREAMING, QUEUED INPUTS, API on Object DBs (MongoDB). Will touch based on this kind of applications with examples.Node.js is programmed in Javascript, so Javascript is used for both front and back end services. It is cleaner and a more consistent codebase. More importantly, it has the largest ecosystem of open source libs.Node js ArchitectureAll browsers have a JS engine which converts javascript code to machine code.  For example, different browsers have different engines like IE uses Chakra, firefox uses SpiderMonkey, chrome uses a V8 engine.In 2009, Ryan Dahl had taken V8 engine out of the browser and embedded it in a C++ program and called it as Node.js. Similar to a browser, node.js provides an environment to run JS engine but it can’t work with document object like a browser for accessing elements in DOM.  Node.js has other capabilities like working with a file system, networking by creating a server etc., which opens a lot of other capabilities.Node.js is not a programming language like C# or a framework like asp.net. It is a run-time environment, which executes javascript code outside the browser. Going further on this topic, it requires some javascript knowledge for which refer to Javascript primer. [Other Document]
logo

Node JS Tutorial

Introduction On Node JS

Node.js is an open-source, cross-platform JavaScript run-time environment (Google Chrome's V8 Engine) that executes JavaScript code outside of a browser. Node.js lets developers use JavaScript to write command-line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser.

Official definition: Node.js is a platform built on Chrome's JavaScript runtime to easily build fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Node.js is quite often used to build back end services like API, which power different client applications like the web app and mobile app running in mobile. Node.js helps to create highly scale scalable, data-intensive and real-time apps.

Why should we use Node.js as there are a lot of other tools and framework for building back-end services like Asp.net, rails, Django etc.?

Node.js is great in Prototyping and agile developments, building super fast and highly scalable services. Some of the companies that use node.js heavily are Netflix, Walmart, PayPal to name few. In fact, PayPal has rebuilt one of its application based on java and spring into node.js. And they ended up building it twice faster with fewer people, 33% fewer lines of code, 40% fewer files, 2x request/sec and 35% faster response time.

Given that many companies use it, let’s understand in detail what makes it useful.

Any typical web application making a service call which needs to fetch/update data in a database in the back end works like this:

User does an action -> application starts processing action -> Makes a DB call -> WILL WAIT TILL REQUEST IS DONE -> Request complete -> Send back result to user.

In case of a multi-thread application, the same scenario is repeated for the multi-thread

  • Request1 -> pick a thread from thread pool -> make DB call -> send response
  • Request2 -> pick a thread from thread pool -> make DB call -> send response
  • Request3 -> pick a thread from thread pool -> make DB call -> send response

As we can see that whenever the thread is waiting for a response from DB, it spends 0% CPU. Ideally, we would like to run some other code when this happens. And in case of multi-threads, we have to allocate the memory for each thread.

However, Node.js being a single-threaded application can still work with 10K concurrent requests. This is because of the event loop mechanism that it has [will learn more about it in later topics]. If we take the same example of multiple requests in node.js case, this is how it works.

  • Request1 -> make DB request
  • Request2 -> make DB request
  • Request3 -> make DB request
  • DB request1 complete -> send response
  • DB request2 complete -> send response
  • DB request3 complete -> send response

If we compare multithreading with node.js, both take roughly the same latency as most of the processing is on the DB side. But the advantage in the case of node.js is that we need not spawn threads, plus the cost of context switching between threads and do lots of mallocs which slows down.

This may sound surprising but node.js being single-threaded is able to match with multithreading because of the event loop and node.js capability to leverage the multi-threading of database. Some of the cases where multithreading fails and node.js wins is when we need to allocate a lot of RAM per thread. And allocating lots of objects which is common in modern web frameworks, will further slowdown. 

This does not mean that we can use node.js in all places. Node.js is not good for intense computations/processing, which does a lot of CPU calculations. And another case is when we are having multi-core servers, we don’t use servers to full capacity as node.js is single-threaded, using only one core. Node.js is not meant for blocking operations and most of the cases of node misuses are because of this.

Some application where node.js suits better are CHAT, BROKERAGE – STOCK TRADER’S DASHBOARD, MONITORING DASHBOARD (and SYSTEM),  DATA STREAMING, QUEUED INPUTS, API on Object DBs (MongoDB). Will touch based on this kind of applications with examples.

Node.js is programmed in Javascript, so Javascript is used for both front and back end services. It is cleaner and a more consistent codebase. More importantly, it has the largest ecosystem of open source libs.

Node js Architecture

All browsers have a JS engine which converts javascript code to machine code.  For example, different browsers have different engines like IE uses Chakra, firefox uses SpiderMonkey, chrome uses a V8 engine.

In 2009, Ryan Dahl had taken V8 engine out of the browser and embedded it in a C++ program and called it as Node.js. Similar to a browser, node.js provides an environment to run JS engine but it can’t work with document object like a browser for accessing elements in DOM.  Node.js has other capabilities like working with a file system, networking by creating a server etc., which opens a lot of other capabilities.

Node.js is not a programming language like C# or a framework like asp.net. It is a run-time environment, which executes javascript code outside the browser. 

Going further on this topic, it requires some javascript knowledge for which refer to Javascript primer. [Other Document]

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

JavaScript Tutorial

JavaScript is a dynamic computer programming language for the web. JavaScript was first known as LiveScript. Later on, Netscape changed its name to JavaScript because of its popularity and the excitement generated by it. JavaScript is lightweight and most commonly used as a part of web pages supported by most web browsers like Chrome, Internet Explorer, Opera, Safari, Edge, and Firefox.
JavaScript Tutorial

JavaScript is a dynamic computer programming language for the web. Jav...

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More