Node JS
Vishal Ranaut
Full Stack Developer | JavaScript, TypeScript, Node.js, Angular, React | Docker & AWS Specialist | Web 3.0 Enthusiast | Innovating with Cutting-Edge Technologies
Since its birth in 1995, JavaScript has established itself as the de facto programming language on the client side of a web application. It executes within a web browser and thereby delivers more dynamic content and makes user experience more interactive. However, to execute code on the server side, we had PHP, ASP, Perl, Python, etc.?Until Node.js, there was no popular JavaScript support on the server side.
Node.js is a JavaScript runtime that can be executed on servers, that is, outside a web browser environment. By default, it's based on Google's V8 JavaScript engine. It's open source and cross platform. It's steered by the Node.js Foundation.
Node.js adopts an event-driven architecture and asynchronous I/O. This enables it to scale better, particularly for real-time web apps or apps that do a lot of I/O. The Node.js ecosystem includes thousands of packages and frameworks.
What's the context for the creation of Node.js?
The web was invented based on the client-server model. Clients (such as web browsers) request the server for a resource. The server serves the content as a response. In this model, the server cannot initiate a response. Even if data has changed on the server, it has to wait for the client to give a request. This was alright when users were passive consumers of information.
When social sites such as Facebook and Twitter arrived, everyone became a potential producer of information. There was a rise of chat apps, multi-user gaming and apps for real-time collaboration. We needed a method for servers to push data to clients.?While Flash and Java Applets enabled this, they were isolated environments that used non-standard ports.
Secondly, typical web servers launch a new thread to handle every client request. This means that scalability is limited by memory and the penalty of switching across threads.
Node.js solves these problems by using single-threaded event-driven processing. It offloads I/O tasks to worker threads and processes them asynchronously via callbacks. Ultimately, this makes it easier for us to develop scalable real-time apps.
What sort of applications can benefit from Node.js?
Node.js is suited for apps that have lots of database accesses or waits on network connections. When there's a need to scale your app to thousands of parallel requests, Node.js does this well without hitting performance limits. On the flip side, Node.js is not suited for CPU-intensive tasks, which will block the main thread.
With Node.js, stream processing of data is possible. Worker threads do this processing.?It's also good for queueing inputs for later batch processing.
Node.js with Socket.IO makes it easy to build real-time apps such as chat apps or games.?It can be used as a proxy to handle many simultaneous connections. Apps with rich real-time dashboards can be created.?Microservices architecture can be implemented with Node.js.
If your codebase spans both client and server concerns, Node.js helps you to write in a single language, JavaScript. This makes it easier to later refactor the code if necessary.?JavaScript is a natural fit for transferring?JSON?data or working with object databases such as MongoDB without the extra processing of encoding or decoding.
How does Node.js work under the hood?
When a Node app starts, callbacks are registered for events. A?callback?is nothing more than executable code that must be called when the event occurs. After this initialization phase, Node enter the?Event Loop. In this loop, Node waits for events to happen and trigger the appropriate callbacks. It's for this reason Node.js is said to use?event-driven programming?paradigm.
For example, a client requests a web page. Event loop will invoke the associated callback. This is?synchronous: event loop will wait for the callback to complete. However, if the callback requires some data from a database, the event loop being a single thread will get?blocked?since I/O is slower compared to CPU. Other events will start queueing up.
To prevent this, Node.js adopts?non-blocking asynchronous I/O: slow tasks will be handed over to the?Worker Pool. This frees the event loop to move on the next event. When a worker completes its task, it will signal an event, at which point, the event loop will process its callback synchronously.
Could you share more details on the event loop?