Node.js - An overview

If server-side programming languages ​​such as C or PHP are used, program code is usually processed sequentially. This means that a server will not begin to translate a new statement in the code until the previous statement has executed and returned a result. One speaks in this case also of a synchronous processing . The execution of further codes is stopped until the current process has ended. This can lead to significant delays in complex operations such as file system access, databases, or web services.

Many programming languages, runtime environments, and implementations based on them therefore support the ability to perform operations in so-called threads in parallel. These are individual threads in a process that allows actions to be executed while the remainder of the code continues to execute. The disadvantage of this method: The more threads are started, the more CPU time and RAM space is required. In other words, multithreading is resource intensive. In addition, additional threads are associated with a much higher programming effort. These problems can be overcome by using a server-side implementation of JavaScript that enables asynchronous execution of program code. The basics are provided by the runtime environment Node.js.

What is Node.js?

Node.js is an event-based architecture software platform that allows JavaScript to be used by the scripting language originally developed for client-side use. This is used in the same way as PHP, Java, .NET, Ruby or Python to write code for the server. Node.js finds use in the development of server-side JavaScript applications that need to handle large amounts of data in real time. The runtime environment for the realization of lightweight web servers is popular.

Launched in 2009 by Ryan Dahl, the cross-platform software project is essentially based on Google’s JavaScript engine V8 , which is also used in the Chrome web browser. Launched by the company Joyent, the project is subordinated since 2015 in the form of the Node.js Foundation to a nonprofit consortium: the Linux Foundation. Current versions are available for Microsoft Windows, Mac OS and Linux.

Node.js contains a library of various JavaScript modules that can be loaded by a simple function and are ready to use as building blocks for web application development. An example is the HTTP module, which makes it possible to create a rudimentary web server with a single function. In addition, additional modules can be installed with the integrated package manager npm (Node Package Manager).

Install Node.js

Depending on the operating system, Node.js can be downloaded from the official website of the software project as an installer and / or binary package. Except for Windows, Linux and macOS for x86 / x64 PCs, the Node.js Foundation provides the software source code and binary packages for ARM, Power and z-Systems platforms. Also versions for AIX and SunOS as well as a Docker image can be downloaded.

Node.js modules

Based on the V8 runtime environment, Node.js lets you write efficient Web servers and other network applications in the popular scripting language JavaScript. Node.js convinces with a deliberately compact design, in which only core functionalities such as the interaction with the operating system, network communication or encryption mechanisms have been integrated into the runtime environment as basic modules . All other functionalities are integrated as extension modules. Users can access an extensive range of third-party modules or program their own modules.

To load any module into a Node.js application, you only need the require () function . This expects a string which specifies as a parameter which module should be loaded. Usually this is the name of the module. If require () is used in combination with a module name, Node.js searches various directories for modules that are part of Node.js as base modules or that were installed with npm project-specific (local) or cross-project (global). These modules are found without a path. The current working directory is not searched. If you would like to load a self-written module from your current project directory, a relative path with ./ is required.

The following examples show both basic schemes for loading modules into Node.js applications:

const modulname = require('modulname'); const modulname = require('./modulname');

Integrate third-party modules

Subsequent installation of program modules takes place at Node.js via the integrated package manager npm . This allows Node.js users to access the npm Registry , a community-based online archive for Node.js modules. Expansion modules do not have to be downloaded manually from external sites and copied to a corresponding directory. The installation effort is limited to a single line of code that is entered into the console of the operating system (in Windows operating systems of the command line interpreter cmd.exe ). All you need is the npm install command and the name of the module to install.

Asynchronous programming with Node.js: callbacks, events and streams

The big advantage of Node.js is the event-driven architecture, with which program code can be executed asynchronously . Node.js relies on single-threading and an outsourced input / output system (I / O) that allows parallel processing of multiple read and write operations.

  • Asynchronous I / O: Classic server tasks include answering queries, storing data in a database, reading files from the hard drive, and establishing connections to other network components. These activities are summarized under the abbreviation “I / O” (input / output). In programming languages ​​such as C or Java, I / O operations are performed synchronously. Thus, one task after the other is processed. The I / O system is blocked until the current task has been completed. Node.js, on the other hand, uses an asynchronous I / O where read and write operations are delegated directly to the operating system or database. This makes it possible to perform a large number of I / O tasks in parallel without blocking (blocking), which in some scenarios gives applications based on Node.js and JavaScript a tremendous speed advantage.
  • Single Threading: To compensate for latency in synchronous I / O, server applications based on classic server-side programming language rely on additional threads – with the above-mentioned disadvantages of a multithreading approach. For example, Apache HTTP Server starts a new thread for each incoming request. The number of possible threads is limited by the available memory – and thus the number of queries that can be answered in parallel in a synchronous multithreading system. Node.js, on the other hand, features only one thread due to the offloaded I / O system, which significantly reduces both complexity and resource utilization.

The asynchronous processing of I / O operations at Node.js is realized by concepts such as callbacks , events and streams .

LEAVE A REPLY

Please enter your comment!
Please enter your name here