Traditional web server model has dedicated thread pool. When a client sent a request in this model then each request is handle by thread from the thread pool. At any point of time if thread is not available in thread pool then request is waits till the next thread available for serving the request. Dedicated thread execute request and returns a response then only return the thread pool.
Node.js Process Model
Node.js processes client request in different way than that of traditional web server model.Node.js has single process hence application code will be running on single thread hence it needs less resources as compare to other platforms. In node.js web application all the client request is handled by single thread and for all I/O work and long running task is performed asynchronously for a request. In this model single thread doesn’t have to wait for request to complete .It is free to handle next request. As soon as asynchronous I/O work completes it will processes the request further and sent the response.
In single thread model an event loop is constantly watching for the events to raise for asynchronous job. It will execute the call back function when the job is completed.Node.js internally uses libev for event loop which in terns uses C++ thread pool for asynchronous I/O.
Asynchronous web server model using Node.js.
Node.js allows us to use non-blocking I/O which manages multiple requests. If at a moment can’t process the request then it effectively executed later without holding up the other thread. Because of this asynchronous nature we can built application’s complex code without adding complexity to code.
Node.js is suited to building micro services:
Micro services have become rapidly growing architecture style .Before micro services monolithic architecture was used which means there is a large container which all other parts of software component are assembled together and tightly coupled.Microservice architecture style is a structure an application as collection of small autonomous services which model around the business model. Each service of micro service is self-contained and implements a single business capability.
In Micro services different devices try to use different services such as build, configure etc.Services are separated based on their domains and functionalities.
Node.js provides the following benefits:
Performance –Node.js is event driven I/O model with micro services strategy which handle extreme amount of load and increase response time.
Developer Friendly –Node.js has vast community support and a lots of developers across the globe. Node package manager (npm) gives millions of ready to module which helps a developers for rapid application development .Node.js enables faster development of application through development life cycle.
Node.js uses more than just web development-As we aware Node.js is flexible. It’s flexibility means it can be used for wide range of different purpose. NPM provides millions of modules for building application for web it is also useful tool for working in ops or infrastructure.Node.js is best tool for developing other development tools. If someone is working to support team of developers to help entire distributed system then Node.js knowledge role could be vital in empowering to build own support tools. Node .js is start using in IOT.
Node.js Community Driven-Node has provided instinct functionality to build different type of application, at a same time it has successful open source community which added additional capabilities of Node.js. One of famous module is Socket.io which is use to manage connection between client and server and allow us to create application which push real time update to client.Socket.io technology used to maintain connections , developer is not bother about it , hence developer is focus on other business logic with less coding. Let us see the example.
Above just 10 lines of code to create Socket code. The client-side code is just as simple, just think of other possibilities! Future writing scalable, real-time applications begins now.
Node.js application hosted anywhere-Recent years there is a considerable exponential growth in Node.js usage. Different cloud-based hosting provider and web server is build on Node such as Google,Amazon,AWS and many more.
Lets now understand node Architecture.
Node.js runtime is a set of executable programs which actually responsible for running Node application. Basically it is consist of (As shown in above figure)
· File System
o Event Loop :It is implemented by using an event driven and non blocking I/O Library which makes it efficient and scalable .
The Node runtime’s infrastructure is has following two major components:
- A non-blocking I/O library : Node provides a non blocking I/O library which is use to handle multiple requests at a time.
Prerequisite of Node.js
Install Node and NPM software:
From node.org download installation platform for your platform and install it in your computer.
Node Module System:
Node.js core module:
Core module include minimum functionality of Node.js is light weight framework .These basic module load automatically when Node.js process is started. In our application we need to import the functionality from the code module.
Below table gives information of Core module important module:
|http||This module includes classes ,event and methods to create http node.js server|
|url||This module includes methods for url resolution and parsing|
|path||This module includes methods deal with file path|
|Fs||This module includes methods ,classes and event to work with file I/O|
|querystring||This module includes methods deal with query string|
|Util||This module gives a utility functionalities for Node.js|
Core Module loading Syntax:
To use Node.js core or NPM modules, you first need to import it using require () function as shown below.
Node.js is trending and opens various possibilities to the developer to leverage to build highly scalable and robust application.
Call the Trainer and Book your free demo class now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.