Node.js is a software platform with event-based architecture, which enables to use JavaScript, the scripting language originally developed for client-side use, on the server side. This means that thanks to Node.js, JavaScript can perform similar tasks than PHP, Java, .Net, Ruby, and Python. Node.je is used for the development of server-side JavaScript applications, which need to manage large amounts of data in real time. It is a particularly popular runtime environment for realizing lightweight web servers.
The development for this cross-platform software project was started by Ryan Dahl in 2009 and it is based on Google’s JavaScript Engine V8, which is also used in the Chrome web browser. Launched by the company, Joyent, the project has been run since 2015 by the Node.js Foundation, a Linux Foundation project. Current versions are available for Microsoft Windows, Mac OS and Linux.
Node.js comprises a library of diverse JavaScript modules, which can be loaded via a simple function call and can be used as ready-made building blocks for developing web applications. One example is the HTTP module, which can create a rudimentary web server with a single function. Additionally, the integrated node package manager (NPM) can be used to install further modules.
One great advantage of Node.js is the event-driven architecture, which allows program code to be executed asynchronously. As Node.js is based on single threading and a non-blocking input/output system (I/O) it allows parallel processing of several read and write operations.
- Asynchronous I/O: some of the most common tasks for servers include answering queries, storing data in a database, reading files from the hard drive, and establishing connections to other network components. These actions can be grouped together under the category ‘I/O’ (input/output). In programming languages like C and Java, I/O operations are executed synchronously. This means that tasks are performed one at a time and the I/O system is blocked until the current task has been completed. Node.js, however, uses an asynchronous I/O, with which read and write operations can be delegated directly to the operating system or a database. This makes it possible for a large number of I/O tasks to be carried out parallel to one another, without leading to a blockage. In some cases, this can prove to be a great advantage when it comes to the speed of the Node.js and JavaScript-based applications.
- Single-Threading: in order to compensate for the synchronous I/O waiting times, server applications written in traditional programming languages use additional threads with the above-mentioned disadvantages of a multithreading approach. An Apache HTTP server, for example, starts a new thread for every incoming query. The available memory limits the number of possible threads and therefore also the number of queries that can be answered in parallel in a synchronous multithreading system. However, because of its non-blocking I/O system, Node.js works with just one thread, which significantly reduces both the complexity as well as the resource load.
- Event-driven architecture: the asynchronous processing of I/O operations is realized through Node.js’s event-driven architecture. This is primarily based on an individual thread, located in a continually running event loop. This event loop has the task of waiting for events and then managing them. Events can exist as either tasks or results. If the event loop registers a task, such as a database query, this will be outsourced to a process in the background via a callback function. For this reason the task is not processed in the thread the event loop runs in, so that the loop can immediately go to the next event. If an outsourced task is being carried out, the results of the outsourced process will be returned to the event loop as a new event via the callback function. As a result, this can prompt the delivery of a result.
A great advantage of an event-driven, non-blocking architecture is that an application based on Node.js is never inactive and waiting for results. It’s therefore possible to carry out different database queries simultaneously without the program stopping, for example. A website structure that requires various external queries can therefore be managed much more efficiently with Node.js than with a synchronous procedure.