Node.js innovations: worth knowing
In this article, we take a look at seven Node.js features that are not yet in the spotlight but still show in which direction the platform is developing. If you want to create a website built with Node.js, this article will also help you to learn more.
With the current version of Node.js, the CommonJS module system is still the standard, and the ECMAScript module system must first be activated.
New applications can be implemented safely with the new modular system, the property “type”: “module” in the package.json file of the application activates the ECMAScript module system throughout the application. Alternatively, starting the application with the command line flag -input-type=module has the same effect.
For existing applications that are still based on the CommonJS module system, the question arises as to how best to migrate the application. There are generally two strategies for this: the first variant is based on a lightweight ECMAScript basic application, in which the existing CommonJS application is integrated and then the modules are migrated step by step to the new module system. The second variant is based on a basic application in CommonJS and integrates ECMAScript modules. Here, too, the individual modules are gradually being converted to the new module system and, finally, the basic application. Both strategies become possible because Node.js provides for interoperability between the two module systems.
The combination of an application based on ECMAScript modules and integrating CommonJS modules is easily possible. The value that the module makes available via module.exports can be treated as a “default export” in the ECMAScript module system.
Node.js uses buffer objects to handle binary data. Many interfaces of the platform use this object class for data exchange. A popular representative is the file system module. The read file function reads the contents of a file. If no encoding, such as utf-8 is specified when the function is called, the contents of the file are available as a buffer object.
The readFile function from the fs module provides a concrete example of the handling of buffers and TypedArrays. If it does not receive an indication of the encoding of the read-out file when called up, it returns a buffer object instead of the contents of the file.
Another purpose of TypedArrays is the data exchange when using worker threads. Unlike child processes, they can access shared storage. Data exchange works here either via ArrayBuffer or SharedArrayBuffer, which in turn works with TypedArrays.
Similar to the URL module, the crypto module is also the case. Node.js has so far also relied on its own implementation here but now also offers an interface that is compatible with the Web Crypto API from the browser. However, this interface is currently still in the experimental stage and should, therefore, not be used productively or should only be used productively with the utmost caution.
Node.js makes the new module available under the web crypto property. The core functions of the Web Crypto module are:
- Working with keys: the Web Crypto API can generate and import existing keys. Keys can also be exported.
- Encryption and decryption: the interface offers methods to encrypt content and thus protect it from unauthorized access. Encrypted content can only be decrypted with a suitable key.
- Signing and verification: the sign method generates a cryptographic signature of a structure that can be checked at a later date. This can be used, for example, to detect unauthorized changes.
The Web Crypto API works with TypedArrays. For example, the data structures to be encrypted or signed must first be converted into a TypedArray. The following example encrypts the string “Hello World” with the encrypt method and then decrypts it again with the decrypt method.
Through the platform’s strategy to avoid breaking changes as much as possible, the inclusion of a new standard always leads through a multi-stage process, at the beginning of which a feature is first hidden behind a feature flag, then it is later generally available as an experimental feature, and finally, as soon as it is stable enough for production use, it is delivered as a stable and thus regular feature. Understandably, these are not all the features worth paying attention to. Remember that it is essential to consult professionals before choosing development tools.