Today, many developers want to learn Node.js programming skills to match the current market and industry needs. Node.js has become very popular. According to the Node.js Foundation Report, there have been more than a billion downloads of Node.js. Why are so many developers choosing Node?
In this article, I will give you a brief summary of Node: what it is; key features; how it works; a list relevant keywords; and, help you understand the big picture as you explore Node and its architecture.
Whenever I learn a new thing, there are three questions I try to answer:
- What is it?
- How does it work?
- When do I use it?
What is Node.js and how does it work?
As per Wiki:
Below are some main features of Node.js:
- Asynchronous and Even Driven: 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, reporting the results of the execution. It’s running on a single thread with the event loop.
- High Scalability: the events mechanism makes Node.js easily scalable.
- It’s very fast: the non-blocking IO system makes Node.js blazing fast.
- Community: Node’s community is very active and always eager to help. With their support, the quality of the packages consistently improves.
- NPM: 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 I am using for building my app.
How Node.js works?
There are 3 key main features of Node.js architecture:
- Single thread: Node.js works with a single thread; so, for any application that requires heavy CPU workload, Node.js may not a good choice.
- Even Loop: It builds on top of “Libuv” which handles queueing and processing of asynchronous events.
- Non-blocking I/O: 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 time; no blocking; this is the way of handling code execution.
What are the differences between single thread and multi-threading? See screenshots below:
You can see with Node.js there is no waiting thread (non-blocking), that is why it’s very fast.
When should you use Node.js?
Node.js is a good choice for applications that have to process a high volume of a short messages 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 Erlang.
Node.js likes real-time applications. Because of its asynchronous nature it is very good for handling real-time data streaming. It can be used to stream media, data from multiple streams, file upload, and it is great for a WebSockets server.
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. Most of our recent projects used AWS Lambda and API Gateway to build microservices, backend services for Single Page Applications and more. With serverless framework, we can easily build, deploy and enhance. It helps to reduce cost; the services are running 24/7 but we only pay when it is used.
When we shouldn’t use Node.js?
We know the benefits of Node.js; however, there are some bad use cases when you shouldn’t consider using it.
As I have mentioned above, Node.js is not a good choice for heavy jobs because it is bad on single thread, non-blocking I/O models because it only uses a single CPU core.
If your application only performs CRUD operations, using Node.js would be superfluous for simple HTML. CRUD doesn’t require more traffic coming to your app.
You have seen many benefits of Node.js, but what about its challenges?
- First of all, because of its asynchronous and callback natures, it can be a little difficult in the early stages of learning Node.js. But don’t worry. If you have a passion for learning new things, you will easily learn Node.js.
- Awful experiences of the callback! Thanks to Promises and now async/await function expression, we can avoid callback-hell and make your code cleaner and easy to understand and maintain. I recently read an article about converting long-chains of Promise.then()’s into async/await automatically. It’s really cool.
- And the last one, of course, is its inability to work well with CPU-intensive tasks.
Node.js is growing quickly but, you should not focus exclusively on Node.js. It is very popular, but you should be looking at other environments to find the one that is most appropriate for your solution needs. Don’t force Node.js into being the only choice you can suggest and apply to your projects. I love using Node.js. I use it nearly every day to build apps, but I don’t use it when there are better choices for my solution needs.
Learn to Share and Share to Learn