node.js

Why use node.js for backend : A complete case study with pros and cons

With so many frameworks available, sometimes it is hard to decide on something new and different. In the case of Node.js, you probably have heard sentences like ‘JavaScript runtime’ or ‘event-driven’ or ‘operates on a single thread event loop’ and have been wondering what it all means.

After over 20 years of stateless-web based on the stateless request-response paradigm, we finally have web applications with real-time, two-way connections. So, what is all this recent excitement around Node.js we’ve been all hearing lately? Why is it so popular? Let’s look at all this and more.

What is Node.js?

The definition of Node.js is quite complex. It’s not a programming language nor is it a framework or a library. On their official website, Node.js is defined as a JavaScript runtime built on Chrome’s V8 JavaScript engine. So Node.js is an open source, a server-side script which runs on the top of Google’s open-source scripting engine V8. Node.js can be simply referred to as a technology.

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. It is mainly used for backend operations and is/can be used together with frontend JavaScript frameworks like AngularReactVue and others. Node.js is similar in design to Ruby’s Event Machine and Python’s Twisted.

How and why did Node.js get popularity?

Node.js was introduced into the world in 2009 by the initial writer Ryan Dahl. Back in 2009, Apache HTTP server was a popular web server. But, it had a major problem; it could not handle a lot of concurrent connections.

When Node.js was created it was able to solve the problem Apache had. It offered scalability and parallel code execution. It introduced a new logic and approach for JavaScript server-side development. It introduced use of a single threaded event loop model. A single thread is used to perform all requests.

Since then its popularity has wildly grown. For instance in Github, Node.js has 81.9k stars and 21.4k forks. In Stackshare it has 96.2k followers and 8.4k votes. These numbers alone can show how popular Node.js is. To the date 7509 companies reportedly use Node.js in their tech stacks, including Uber, Netflix, Paypal and Twitter.

Why Node.js is so popular among developers.

  1. Easy to learn.

This is one of the major contributors to its wide usage. Among many developers, JavaScript skills are common. Node.js uses JavaScript. So, learning Node.js is relatively easy and few weeks of learning can get you up and running. Without JavaScript Knowledge, learning Node.js will take a bit longer but still manageable. Therefore, learning Node.js is not that tricky.

  1. The scalability offered.

Node.js is designed with scalable network applications in mind. Node.js can handle many concurrent requests. This is the main reason it quickly became popular among developers and large companies. It can handle many simultaneous requests without straining the server. A callback is fired only after a request, but if there are no more jobs to be done, Node.js will go to sleep. Node.js is only online when needed. This avoids unnecessary usage of your RAM.

  1. The Mobile-friendly, Cross-platform, and Dev-Friendly nature.

The ability to build flexible apps that run smoothly on any platform, be it Linux, Windows, or Mac OS gives Node.js a big thumbs up. There has been a rise in cross-platform app development in the last few years. The fear of apps not running on other operating systems is gone, as Node.js helped take care of that. It has enabled developers to write apps that run on different platforms with little or no modifications being made.

  1. Lightweight and fast.

Node.js uses Google’s V8 JavaScript engine which is built in C++. The engine is an open source and high-performance JavaScript and Web Assembly engine. The engine was developed for Google Chrome and Chromium browsers by The Chromium Project. V8 engine handles memory allocation, compiles, and executes JavaScript. It compiles JavaScript into machine code using a just-in-time (JIT) compiler before executing it.

More about Google’s V8 JS engine can be found in the V8 docs. Node.js uses a non-blocking model in performing operations. Node.js has published a full article on blocking and non-blocking models here. It handles requests made in a single asynchronous thread. This reduces CPU workloads as well as memory. This makes your app lightweight.

  1. The many hosting providers available.

As a result of its growth in recent years, many cloud-based hosting providers and web servers have added quite simple ways of hosting Node.js code. Some of these hosting providers include Heroku, Amazon Web Services, Azure and Digital Ocean among others. There are also many detailed and simple guides on this.

  1. Highly extensible

Node.js does not provide everything that you need all at once. Instead, you customize or add what you need over time. This prevents having unnecessary libraries. NPM – Node package manager is the default package manager in Node.js. Here, you can browse and install over 1 million open source packages. You can also use it to identify and install project dependencies.

  1. Its caching ability

Node.js provides the ability to cache single modules. Caching is the storage of data for future requests. When a request involving the module is made, it is fetched from the cache instead of the main servers. Thus, the app loads faster and responds quicker.

What apps we can develop with Node.js?

You may be asking, what kind of apps we can develop with Node.js. The answer is: Node.js can be used to make almost any kind of app. Below are some of the apps you can develop using Node.js.

  • Single-page applications (SPA). e.g. portfolio etc.
  • Social media applications. LinkedIn uses Node.js.
  • Real-time apps like chat apps.
  • Streaming apps. For example, Netflix.
  • Ecommerce apps. For example, Walmart.
  • Blogs.
  • Online Payment systems. For example, PayPal
  • APIs
  • Mobile apps. Node.js can be used together with other frameworks eg. ExpressJs to build mobile apps.
  • Many more.

The advantages of using Node.js.

  • Node.js modules and tools are easily available via NPM.
  • Node.js developers are easily available.
  • Strong community and bug tracking team.
  • It can be used to build a wide range of apps.
  • It’s Cross-platform and mobile-friendly.
  • Hosting Node.js code is not a hassle due to many of its supported hosting platforms.
  • It’s lightweight.
  • It’s ability to handle many simultaneous requests.
  • Fast code execution.

What are the disadvantages of Node.js?

  • Decreased performance during complex & heavy computation tasks

As we have mentioned before, Node.js is single-threaded and event-driven, and that is why it isn’t suited for heavy computational tasks. Receiving a massive computational task, it uses the CPU power to the fullest to handle that task, leaving other tasks running in a queue. That means slowing down the entire thread of events, which impedes the interface. “Worker threads” have been introduced to remedy this problem, but this solution isn’t absolutely effective at handling CPU-related computational tasks.

  • Callback hell puts code quality at risk

Node.js is reliant on callbacks due to its asynchronous approach. Typically, this function works after executing a task. If multiple tasks are running in the background, each having a callback, it might cause callback hell, an unwieldy number of nested “if” statements or functions. It impedes performance and lowers the quality of code.

  • Lack of library support

While the npm library seems rich, the quality of many packages leaves much to be desired. Lots of them don’t have proper documentation. Since it’s an open source system, professional monitoring here is scant, so many packages don’t meet the coding standards. The good news is that Joyent supervises the key Node.js technologies.

Conclusions:

Node.js can be used for applications of various sizes and needs thanks to its high level of scalability. By offering security, scalability and performance among other things, Node.js has become a solution to many problems. It is a type of technology that works without any unnecessary effort.

Node.js is not a one-size-fits-all solution, but it’s a good solution for creating fast and scalable web applications with less engineering effort.

Node.js is perfect for creating streaming web applications, server-side apps or microservices. Being built on Chrome’s V8 engine, using asynchronous execution and operating on a single thread event loop Node is recognized as an amazing and scalable framework for many businesses.