What is Node.js and why it becomes more popular?
Tags: node, javascript
Published: 2019-10-28 10:51:18

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1203/1*9c31nbJ0k02k8fxOSkbfPg.png" alt=""/><figcaption><a rel="noreferrer noopener" href="https://foundation.nodejs.org/" target="_blank">A source from Node.js Foundation</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Nowadays, many developers wanted to learn Node.js programming skills to match the current market and industry needs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Node.js became very popular over the past few years. According to the Node.js Foundation report,&nbsp;more than a billion download&nbsp;of Node.js. So what are the reasons that make many developers choose Node? In this article, I will give you a brief summary of Node, what it is, key features, how it works and list out relevant keywords that I think we should take a look at and understand the big picture if you want to deeply explore Node and its architecture.</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:paragraph -->
<p>Whenever I learn a new thing, there are three questions I will try to get the answer:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>What</strong>&nbsp;it is?</li><li><strong>How</strong>&nbsp;does it work?</li><li><strong>When</strong>&nbsp;do we use it?</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Let’s begin…</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What is Node.js and how does it work?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As per Wiki</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><strong>Node.js</strong>&nbsp;is an&nbsp;<a href="https://en.wikipedia.org/wiki/Open-source_software" target="_blank" rel="noreferrer noopener">open-source</a>,&nbsp;<a href="https://en.wikipedia.org/wiki/Cross-platform" target="_blank" rel="noreferrer noopener">cross-platform</a>&nbsp;<a href="https://en.wikipedia.org/wiki/JavaScript" target="_blank" rel="noreferrer noopener">JavaScript</a>&nbsp;<a href="https://en.wikipedia.org/wiki/Runtime_system" target="_blank" rel="noreferrer noopener">run-time environment</a>&nbsp;that executes JavaScript code outside of a browser. Historically, JavaScript was used primarily for&nbsp;<a href="https://en.wikipedia.org/wiki/Client-side_scripting" target="_blank" rel="noreferrer noopener">client-side scripting</a>, in which scripts written in JavaScript are embedded in a webpage’s HTML and run client-side by a JavaScript engine in the user’s web browser.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>Below are some main features of Node.js:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><strong>Asynchronous and Event Driven:</strong>&nbsp;Node doesn’t wait for results and doesn’t block other calls. Whenever it receives a request, it will immediately handle the request, after it finishes executing it will run a callback to notify the results of the execution. It’s running on a single thread with the event loop.</li><li><strong>High Scalability</strong>: the events mechanism make Node.js can be easily scalable.</li><li><strong>It’s very fast:&nbsp;</strong>the non-blocking IO system makes Node.js blazing fast.</li><li><strong>Community</strong>: the Node’s community is very active and always eager to help. With their support, the quality of the packages consistently improves.</li><li><strong>NPM</strong>: likes Maven, NuGet, or Ruby Gems, it’s a tool that handles installing and updating of reusable modules from the online collection. It manages the version and dependencies of the reusable modules that we are using for building our app.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>How Node.js works?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>There are 3 key main features of Node.js architecture:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><strong>Single thread</strong>: since it works with a single thread so for any application requires heavy CPU workload, Node.js may not a good choice.</li><li><strong>Even Loop</strong>: It builds on top of “Libuv” which handles queueing and processing of asynchronous events.</li><li><strong>Non-blocking I/O</strong>: event loop works on a single thread, but all long-running tasks (network I/O, data access, etc.) are always executed asynchronously on top of the worker thread which returns results via a callback to the event loop thread. No wait, no blocking, this is the way of handling code execution.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>So what are differences between single thread and multithreading? See some screenshots below:</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://miro.medium.com/max/2188/1*WVP8haZ_BXxVPEzsPDZWsQ.png" alt=""/><figcaption><a rel="noreferrer noopener" href="https://strongloop.com/strongblog/node-js-is-faster-than-java/" target="_blank">Image Source</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://miro.medium.com/max/2228/1*uqKyhnkBDHT9jdEWQaMv_g.png" alt=""/><figcaption><a rel="noreferrer noopener" href="https://strongloop.com/strongblog/node-js-is-faster-than-java/" target="_blank">Image Source</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can see with Node.js there is no waiting thread (non-blocking), that is why it’s very fast.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>When you should use Node.js?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Real-time applications</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Node.js is a good choice for applications that have to process a high volume of a short message requiring low latency. Such systems are called real-time applications (RTA). However, if you intend to build heavy real-time applications, I suggest having a try with&nbsp;<a href="https://www.erlang.org/" target="_blank" rel="noreferrer noopener"><strong>Erlang</strong></a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Data streaming</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Likes real-time applications, because of asynchronous nature, it’s very good for handling real-time data streaming. It can be used to stream media, data from multiple streams, file upload, or it’s great for&nbsp;<a href="https://www.merixstudio.com/development/websocket/" target="_blank" rel="noreferrer noopener">WebSockets</a>&nbsp;server</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>API Server</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Because it can handle many concurrent connections at once, it’s suitable for API service. The JSON data is used naturally ob JavaScript, therefore you can easily convert JS objects into JSON format. It is a good choice to build backend services for&nbsp;<a href="https://en.wikipedia.org/wiki/Single-page_application" target="_blank" rel="noreferrer noopener">Single Page Application</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Microservices</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Node.js is well suited to act as microservices. Because it’s fast and lightweight it can be used for writing microservices that easily scale. Almost our recent projects used AWS Lambda and API Gateway to build microservices, backend services for Single Page Application, and more. With the&nbsp;<a rel="noreferrer noopener" href="https://serverless.com/" target="_blank"><strong>Serverless</strong></a>&nbsp;framework, we can easy to build, deploy and enhance. It helps to reduce cost, the services are running 24/7 but we only pay when it is used.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>When we shouldn’t use Node.js?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We know the benefits of Node.js, how fast it is, however, there are still some bad use cases when you shouldn’t consider using it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>CPU-heavy jobs</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As I have mentioned above, Node.js is not a good choice for heavy jobs because it is bad on a single thread, non-blocking I/O model, but it only uses a single CPU core.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>CRUD</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If your application only perform CRUD operation, using Node.js would be superfluous for simple HTML, CRUD doesn’t require more traffic coming to your app.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>You have seen many benefits of Node.js, but does it have disadvantages.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li>First of all, because of asynchronous and callback natures, it makes a little difficult for the first time we learn Node.js. But don’t worry, I believe if you have passion for learning new things, you will easy to catch-up and learn a lot.</li><li>Awful experience of the callback! Thanks to Promises and now async/await function expression, we can avoid&nbsp;<a rel="noreferrer noopener" href="http://callbackhell.com/" target="_blank">callback-hell&nbsp;</a>and make your code cleaner and easy to understand and maintain. I recently read an article about&nbsp;<a rel="noreferrer noopener" href="https://dev.to/ben/visual-studio-code-can-now-convert-your-long-chains-of-promisethens-into-asyncawait-automagically-1b1b" target="_blank">converting long-chains of Promise.then() into async/await automatically</a>. It’s really cool.</li><li>And the last one, of course, it is not working well for CPU-intensive tasks.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":1} -->
<h1 id="a69a">Summary</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Node.js is growing quickly, and others also growing every day. I recommend you should not only focus on Node.js, but you should also look at the others, find out the best one that appropriates for your solution, don’t make Node.js is only one choice that you can suggest and apply to your projects.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I personally love using Node.js and I use every day to build the apps. I will have more articles coming up to show you how we use Node.js. I am willing to see your comments to help to improve my writing skills as well as technical skills.</p>
<!-- /wp:paragraph -->

Visit our [blog](https://innomizetech.com/blog) for more interesting articles. If you have any questions or need help you can contact me via [Twitter](https://twitter.com/hoangleitvn).