NodeJs

 


What is NodeJS?

NodeJS is an open source, cross platform runtime environment for server-side and networking applications. And also it is free. NodeJS runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.). It is developed by Ryan Dahl. And created with the aim of creating real-time websites with push capabilities (websockets). NodeJS runs on V8 engine, the core of Chrome's JavaScript engine, outside of the browser, single threaded, non-blocking. NodeJS uses JavaScript on the server, 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.

"NodeJS uses asynchronous programming".

  • NodeJS comes with several JavaScript libraries that help basic programming.
  • 'npm' is the largest in the world for open source libraries.
Example: NodeJS Application



Features of NodeJS

  • Asynchronous and Event Driven All APIs of Node.js library are asynchronous, that is, non-blocking. It essentially means a Node.js based server never waits for an API to return data.
  • Very Fast - Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.
  • Single Threaded but Highly Scalable - NodeJS uses a single threaded model with event looping.
  • No Buffering - NodeJS applications never buffer any data.
  • License - Node.js is released under the MIT License.

What NodeJS can do?

  • can generate dynamic page content
  • can create single threaded applications
  • suitable for heavy I/O operations applications
  • can create, open, write, delete and close file on the server
  • can collect form data
  • can add, retrieve, modify and delete data in your database

Event Loop


Event loop is one of the most important aspects to understand about NodeJS.

The event loop allows NodeJS to perform non-blocking I/O operations despite the fact that JavaScript is single threaded. 

It is done by assigning operations to the operating system whenever and whenever possible.

The most modern kernels are multi threaded. They can handle multiple execution at a one time in a background.

When one of these operations completes, the kernel tells NodeJS so that the appropriate callback may be added to the poll queue to eventually be executed.

Features of event loop

  • Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks.
  • The event loop allows us to use callbacks and promises.
  • The event loop executes the tasks starting from the oldest first.
Example of event loop:

console.log("This is the first statement");

setTimeout(function(){
console.log("This is the second statement");
}, 1000);

console.log("This is the third statement");

Advantages and Disadvantages of event loop

Advantages

  • Easy to learn
  • Easy Scalability
  • Easy Extendible
  • High Performance
  • Fullstack JavaScript
  • Handles the requests simultaneously
  • Large Community
  • Support common tools like unit testing
  • Have 'npm' package manager. It has lot of reusable libraries/ modules.
  • Offers the freedom to develop apps
  • Getting support for commonly used tools
  • Advantage of Caching

Disadvantages

  • API interface is not stable
  • Not good for heavy CPU operations projects 
  • Not good with relational databases
  • Asynchronous programming models complex than synchronous programming models

Node Package Manager (NPM)


  • npm is the world's largest Software Registry. 
  • The registry contains over 800,000 code packages.
  • Open source developers use npm to share software.
  • npm is free to use, can download all npm public software packages without any registration or login.
  • npm have CLI (Command Line Client) that can be used to download and install software.
  • All npm packages are defined in files called package,json, it's content must be written in JSON.
  • npm can manage all dependencies.
Example:

{
"name" : "foo",
"version" : "1.2.3",
"description" : "A package for fooing things",
"main" : "foo.js",
"keywords" : ["foo", "fool", "foolish"],
"author" : "John Doe",
"licence" : "ISC"
}


Comments

Popular posts from this blog

NoSQL