What is Node.js and it's uses ?

What is Node.js and it's uses ?

Introduction

Node.js is an open source development platform for executing JavaScript code on server-side. Node is useful for developing applications that require a persistent connection from the browser to the server and is often used for real-time applications such as chat, news feeds and web push notifications.

Node is primarily used for non-blocking, event-driven servers, due to its single-threaded nature. ?Node runs on chrome v8 engine which converts javascript code into machine code, it is highly scalable, lightweight, fast, and data-intensive.

Node accepts the request from the clients and sends the response, while working with the request node handles them with a single thread. To operate I/O operations or requests node use the concept of threads. Thread is a sequence of instructions that the server needs to perform. It runs parallel on the server to provide the information to multiple clients.?Node is an event loop single-threaded language.?It can handle concurrent requests with a single thread without blocking it for one request.

Where is Node Used ?

No alt text provided for this image

Node.js is commonly used to develop real-time applications like chat applications, streaming application, command-line application, browser games.

Few Examples of Real-time applications:-

  1. Netflix:- Netflix is a streaming service.
  2. LinkedIn:- LinkedIn is a professional networking site. It is the main technology that supports the backend of the LinkedIn app. LinkedIn serves as business and service employment-oriented services.
  3. Uber:- Uber is a platform that connects cab drivers to customers. Because of its nature of processing large amounts of data and convenient error analysis, it was chosen as the backend technology.
  4. PayPal:- PayPal is an online payment platform and has over 200 million active user accounts.

Platform Architecture

No alt text provided for this image

To manage several concurrent clients, Node.js employs a “Single Threaded Event Loop” design. The JavaScript event-based model and the JavaScript callback mechanism are employed in the Node.js Processing Model.?It employs two fundamental concepts:??

  1. Asynchronous model :- An asynchronous model allows multiple things to happen at the same time. When you start an action, your program continues to run. When the action finishes, the program is informed and gets access to the result.
  2. Non-blocking of I/O operations :- Non-blocking I/O operations allow a single process to serve multiple requests at the same time. Instead of the process being blocked and waiting for I/O operations to complete, the I/O operations are delegated to the system, so that the process can execute the next piece of code

Components of the Node.js Architecture:

  • Requests :- Incoming requests can be blocking (complex) or non-blocking (simple), depending upon the tasks that a user wants to perform in a web application.
  • Node.js Server :- Node.js server is a server-side platform that takes requests from users, processes those requests, and returns responses to the corresponding users.
  • Event Queue :- Event Queue in a Node.js server stores incoming client requests and passes those requests one-by-one into the Event Loop.
  • Thread Pool :- Thread pool consists of all the threads available for carrying out some tasks that might be required to fulfill client requests.
  • Event Loop :- Event Loop indefinitely receives requests and processes them, and then returns the responses to corresponding clients.
  • External Resources :- External resources are required to deal with blocking client requests. These resources can be for computation, data storage, etc.

Node Package Manager

No alt text provided for this image

npm is a package manager for the JavaScript programming language maintained by npm, Inc. npm is the default package manager for the JavaScript runtime environment Node.js. It consists of a command line client, also called npm, and an online database of public and paid-for private packages, called the npm registry.

npm consists of three distinct components:

  1. The Website
  2. The Command Line Interface (CLI)
  3. The Registry

Basically, npm command is used to download package which makes it easier write small code by using packages.

The Node.js Modules

CommonJS modules are the original way to package JavaScript code for Node.js. Node.js also supports the ECMAScript modules standard used by browsers and other JavaScript runtimes.

No alt text provided for this image

There are plenty of Node.js Modules to use. Check it out below.

Pros and Cons

No alt text provided for this image

Pros:-

Robust technology stack :- JavaScript has proven to be an undisputed leader among the most popular programming languages. In turn, Node.js has become a stand-alone name in the industry. With a total of 368,985,988 downloads and over 750 new contributors as stated in Node-by-numbers report 2018, the project seems to be stronger than ever.

Fast-processing and event-based model :- Node.js is fast; it is not a myth. Take a look at the?performance tests by toptal.com, comparing how GO, PHP, Java, and Node.js handle concurrent requests. There a couple of reasons for Node.js showing such results:

The engine used in Node.js implementation was originally developed for the Chrome browser. Written in C++, Chrome’s V8 is used to compile functions written in JavaScript into machine code, and it does the job at an impressive speed. Just check performance benchmarks in?V8’s blog. Thanks to Google investing heavily in its engine, V8 demonstrates performance improvements every year, and Node.js extracts the whole bag of benefits out of it.

Non-blocking Input/Output and asynchronous request handling made Node.js capable of processing requests without any delays. In the context of backend, synchronous processing assumes that code is executed in a sequence. Thus, each request blocks a thread, making other requests wait for it to be finished. Asynchronous processing allows requests to be processed without blocking (non-blocking I/O) the thread. So after a request is processed, it can push out a callback and continue serving requests. That helps Node.js make the most of single threading, resulting in short response time and concurrent processing.

Scalable technology for microservices :- Since it’s a lightweight technology tool, using Node.js for microservices architecture is a great choice. This architectural style is best described by Martin Fowler and James Lewis as “an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API.”

Accordingly, breaking the application logic into smaller modules, microservices, instead of creating a single, large monolithic core, you enable better flexibility and lay the groundwork for further growth. As a result, it is much easier to add more microservices on top of the existing ones than to integrate additional features with the basic app functionality.

Rich ecosystem :- One word – npm, a default Node.js package manager, it also serves as a marketplace for open source?JavaScript tools, which plays an important role in the advance of this technology. With about?836,000 libraries available?in the npm registry as of now, and over 10,000 new ones being published every week, the Node.js ecosystem is quite rich. The same stats point out that 97 percent of modern web applications consist of npm modules. And that’s proof of its undisputable popularity among developers.

With such a vast variety of free tools accessible in a few clicks, there is a huge potential for the use of Node.js. At the same time,?open source software?enjoys growing popularity as it allows you to build new solutions reducing the overall costs of development and time to market.

Strong corporate support :- As mentioned above, the development of Node.js was supported by Joyent. In 2015, the Node.js Foundation was created to “enable widespread adoption and help accelerate the development of Node.js.” IBM, Microsoft, PayPal, Fidelity, and SAP became the founding members of the organization.

The list of organizations using Node.js in production is constantly growing. It currently includes almost three hundred well-known companies, such as PayPal, Medium, Trello, Uber, and Zendesk.

6. Seamless JSON support :- Although other backend technologies like PHP and Ruby on Rails can use JSON format for communication, Node.js does it without converting between binary models and uses JavaScript. This is especially handy when you need to build RESTful APIs for NoSQL database support, like MongoDB – the letter M in the MEAN stack. This seamless communication with one of the main data transfer standards is another advantage of the JavaScript ecosystem.

Cons :-

Performance bottlenecks with heavy computation tasks :- The biggest drawback of Node.js even now is its inability to process CPU bound tasks. But, to understand what the roots of this issue are, we need a little bit of context. Let’s begin with the basics, with JavaScript itself.

As we know, Node.js is a runtime environment that executes JavaScript on the server side. Being a frontend programming language, JavaScript uses a single thread to process tasks quickly. Threading is not required for it to work, because tasks in JavaScript are lightweight and take little CPU.

Turning back to Node.js, now we know why it is considered single threaded: It processes JavaScript, which is single threaded. A non-blocking input/output model means that Node.js answers the client call to start a request and processes the task during the time it shoots callback, as the task is ready. Processing tasks asynchronously, Node executes JS code on its single thread on an event basis. That is what called an event-loop.

The problem occurs when Node.js receives a CPU bound task: Whenever a heavy request comes to the event loop, Node.js would set all the CPU available to process it first, and then answer other requests queued. That results in slow processing and overall delay in the event loop, which is why Node.js is not recommended for heavy computation.

Callback hell issue :- Due to its asynchronous nature, Node.js relies heavily on callbacks, the functions that run after each task in the queue is finished. Keeping a number of queued tasks in the background, each with its callback, might result in the so-called callback hell, which directly impacts the quality of code. Simply put, it’s a “situation where callbacks are nested within other callbacks several levels deep, potentially making it difficult to understand and maintain the code.”

Immaturity of tooling :- Although the core Node.js modules are quite stable and can be considered mature, there are many tools in the npm registry which are either of poor quality or not properly documented/tested. Moreover, the registry itself isn’t structured well enough to offer the tools based on their rating or quality. Hence it might be difficult to find the best solution for your purposes without knowing what to look for.

The fact that the Node.js ecosystem is mostly open source, has its impact as well. While the quality of the core Node.js technology is supervised by Joyent and other major contributors, the rest of the tools might lack the quality and high coding standards set by global organizations.

Growing demand for experienced professionals :- Despite a common belief, not all JavaScript developers are Node.js developers as well. Mastering server-side JavaScript programming requires a significant amount of effort and a certain background in backend development. Due to such a steep learning curve, the number of Node.js engineers is significantly lower than the total number of JS professionals.

As the hype over Node.js continues to grow, the demand for experienced professionals in this field increases.

Thus, with millions of JavaScript developers out there, it might be hard to find a skilled Node.js professional for your project. In this case, you certainly don’t want to limit your search to only one country. Sourcing technical talent overseas has long become the norm in the IT industry.

Resources to Learn Node.js

Watch tutorials on Youtube, W3Schools,etc.

Node.js Learn Section :-

Node.js Documentation :-


Thanks For Reading??.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了