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.
What is 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.
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:
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.
- 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 .