Node Js

Home Node Js

What is Node.js?

Node.js is a server-side runtime environment that runs on Chrome’s V8 JavaScript engine. Node.js may be run outside of the browser and is thus very performant. It is open-source under the MIT license and has cross-platform functionality meaning that it may be used on Windows, MAC, and Linux. At its core, Node.js is event-driven and possess asynchronous behavior by default. We will see that this asynchronous, or non-blocking, behavior is key in advantages over the traditional web server model.

Advantages of Node.js

The popularity of Node.js can be attributed to a couple of stand-out advantages, some of them alluded to in the previous section.
  • Node.js is open-source meaning it has been made freely available to developers all over the world. Anyone is able to proposed modifications or improvements.
  • Using Node.js we are able to build entire server-side applications using only JavaScript.
  • The runtime environment itself is very lightweight. It includes only the core modules needed to function. Any other modules required by the application may be easily installed.
  • The asynchronous nature of Node.js leads to increased performance over other frameworks.
  • Node.js may be run on Windows, MAC, or Linux systems.

The Web Server Model

The traditional web server model consists of a pool of threads which may process requests. Each time a new request comes in, it is assigned to a different thread in the pool. In the event, a request is received and a thread is not available, the request will have to wait until a previous request finishes, a response is returned, and the thread is returned to the thread pool. In this way, the web server model is synchronous or blocking.

The Node.js Process Model

The Node.js process model differs from traditional web servers in that Node.js runs in a single process with requests being processed on a single thread. One advantage of this is that Node.js requires far fewer resources. When a request comes in, it will be placed in an event queue. Node.js uses an event loop to listen for events to be raised for an asynchronous job. The event loop continuously runs, receiving requests from the event queue.

There are two scenarios that will occur depending on the nature of the request. If the request is non-blocking, it does not involve any long-running processes or data requests, the response will be immediately prepared and then sent back to the client. In the event the request is blocking, requiring I/O operations, the request will be sent to a worker thread pool. The request will have an associated call-back function that will fire when the request is finished and the worker thread can send the request to the event loop to be sent back to the client. In this way, when the single thread receives a blocking request, it hands it off so that the thread can process other requests in the meantime. In this way Node.js is inherently asynchronous.

Start Your Project In Node.Js