Our Global Presence
Canada
57 Sherway St,
Stoney Creek, ON
L8J 0J3
India
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
USA
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
Node.js major release is rolled out every six months. The new release becomes the Current release for six months, which gives library authors time to add support for them.
After six months, odd-numbered releases, such as 19, become unsupported, and even-numbered releases, such as 18, move to the Active LTS (long-term support) status and are ready for general use.
LTS release typically guarantees that critical bugs will be fixed for a total of 30 months. Production applications should only use Active LTS or Maintenance LTS releases.
Node.js 19 was released recently which comes with 6 major features:
Let’s explore what they are and how to use them.
Run the command to install node 19.0.0:
% nvm install 19.0.0 Downloading and installing node v19.0.0... Downloading https: //nodejs.org/dist/v19.0.0/node-v19.0.0-darwin-x64.tar.xz... Computing checksum with sha256sum Checksums matched! Now using node v19.0.0 (npm v8.19.2) |
On any window, run the command to use node 19:
% nvm use 19 Now using node v19.0.0 (npm v8.19.2) |
Now you’re ready to explore:
% node --version v19.0.0 |
In A Hands-on Guide for a Server-Side Rendering React 18 App, you have to build a production Create React App by executing npm run build.
You can created server/index.js:
const express = require ( "express" ); const path = require ( "path" ); const app = express(); app. use (express. static (path.join(__dirname, "../build" ))); app.listen(8080, () => console.log( "Express server is running on localhost:8080" ) ); |
The server was running with nodemon, a tool that helps to develop Node.js applications by automatically restarting the application when file changes are detected. The command is nodemon server.
With node.js 19, you no longer need to install the additional tool. Instead, you can execute node –watch to automatically restart the application when file changes are detected.
% node --watch server (node:67643) ExperimentalWarning: Watch mode is an experimental feature. This feature could change at any time (Use `node --trace-warnings ...` to show where the warning was created) Express server is running on localhost:8080 |
Node.js 19 sets keepAlive to true by default. This means that any outgoing HTTP(s) connection will automatically use HTTP 1.1 keepAlive. The default keepAlive duration is 5 seconds.
Change the above server/index.js to:
const http = require ( 'node:http' ); console.log(http.globalAgent); const https = require ( 'node:https' ); console.log(https.globalAgent); |
Execute the server code using node.js 16:
% nvm use 16 Now using node v16.0.0 (npm v7.10.0) % node server Agent { _events: [Object: null prototype] { free: [Function (anonymous)], newListener: [Function: maybeEnableKeylog] }, _eventsCount: 2, _maxListeners: undefined, defaultPort: 80, protocol: 'http:' , options: [Object: null prototype] { path: null }, requests: [Object: null prototype] {}, sockets: [Object: null prototype] {}, freeSockets: [Object: null prototype] {}, keepAliveMsecs: 1000, keepAlive: false, maxSockets: Infinity, maxFreeSockets: 256, scheduling: 'lifo' , maxTotalSockets: Infinity, totalSocketCount: 0, [Symbol(kCapture)]: false } Agent { _events: [Object: null prototype] { free: [Function (anonymous)], newListener: [Function: maybeEnableKeylog] }, _eventsCount: 2, _maxListeners: undefined, defaultPort: 443, protocol: 'https:' , options: [Object: null prototype] { path: null }, requests: [Object: null prototype] {}, sockets: [Object: null prototype] {}, freeSockets: [Object: null prototype] {}, keepAliveMsecs: 1000, keepAlive: false, maxSockets: Infinity, maxFreeSockets: 256, scheduling: 'lifo' , maxTotalSockets: Infinity, totalSocketCount: 0, maxCachedSessions: 100, _sessionCache: { map: {}, list: [] }, [Symbol(kCapture)]: false } |
Execute the server code using node.js 19:
% nvm use 19 Now using node v19.0.0 (npm v8.19.2) % node server Agent { _events: [Object: null prototype] { free: [Function (anonymous)], newListener: [Function: maybeEnableKeylog] }, _eventsCount: 2, _maxListeners: undefined, defaultPort: 80, protocol: 'http:' , options: [Object: null prototype] { keepAlive: true, scheduling: 'lifo' , timeout: 5000, noDelay: true, path: null }, requests: [Object: null prototype] {}, sockets: [Object: null prototype] {}, freeSockets: [Object: null prototype] {}, keepAliveMsecs: 1000, keepAlive: true, maxSockets: Infinity, maxFreeSockets: 256, scheduling: 'lifo' , maxTotalSockets: Infinity, totalSocketCount: 0, [Symbol(kCapture)]: false } Agent { _events: [Object: null prototype] { free: [Function (anonymous)], newListener: [Function: maybeEnableKeylog] }, _eventsCount: 2, _maxListeners: undefined, defaultPort: 443, protocol: 'https:' , options: [Object: null prototype] { keepAlive: true, scheduling: 'lifo' , timeout: 5000, noDelay: true, path: null }, requests: [Object: null prototype] {}, sockets: [Object: null prototype] {}, freeSockets: [Object: null prototype] {}, keepAliveMsecs: 1000, keepAlive: true, maxSockets: Infinity, maxFreeSockets: 256, scheduling: 'lifo' , maxTotalSockets: Infinity, totalSocketCount: 0, maxCachedSessions: 100, _sessionCache: { map: {}, list: [] }, [Symbol(kCapture)]: false } |
Enable keepAlive will deliver better throughput as connections are reused by default.
Additionally, the agent is able to parse the response keepAlive that the servers might send. This header instructs the client on how much longer to stay connected.
On the other side, the HTTP server will automatically disconnect idle clients when close() is invoked. It is accomplished by http(s).Server.close calling closeIdleConnections internally.
With these changes, HTTP(S)/1.1 requests may experience a better throughput/performance by default.
The WebCrypto API is an interface to build systems using cryptography. With node.js 19, the WebCrypto API is stable (with the exception of these algorithms: Ed25519, Ed448, X25519, and X448).
You can use globalThis.crypto or require(‘node:crypto’).webcrypto to access this module. The following server/index.js use subtle as an example, where the SubtleCrypto interface provides a number of low-level cryptographic functions:
const { subtle } = globalThis.crypto; (async function () { const key = await subtle.generateKey({ name: 'HMAC' , hash: 'SHA-256' , length: 256 }, true, [ 'sign' , 'verify' ]); console.log( 'key =' , key); const enc = new TextEncoder(); const message = enc.encode( 'I love cupcakes' ); console.log( 'message =' , message); const digest = await subtle.sign({ name: 'HMAC' }, key, message); console.log( 'digest =' , digest); })(); |
The following console information shows the values of key, message, and digest:
% node server key = CryptoKey { type: 'secret' , extractable: true, algorithm: { name: 'HMAC' , length: 256, hash: [Object] }, usages: [ 'sign' , 'verify' ] } message = Uint8Array(15) [ 73, 32, 108, 111, 118, 101, 32, 99, 117, 112, 99, 97, 107, 101, 115 ] digest = ArrayBuffer { [Uint8Contents]: <30 01 7a 5c d9 e2 82 55 6b 55 90 4f 1d de 36 d7 89 dd fb fb 1a 9e a0 cc 5d d8 49 13 38 2f d1 bc>, byteLength: 32 } |
Node.js has removed the –experimental-specifier-resolution flag, because its functionality can be achieved via custom loaders.
Clone the example repository:
git clone https://github.com/nodejs/loaders-test.git
Go to the example directory:
% cd loaders-test/commonjs-extension-resolution-loader
Install the packages:
% yarn install
Here is loaders-test/commonjs-extension-resolution-loader/test/basic-fixtures/index.js:
import { version } from 'process' ; import { valueInFile } from './file' ; import { valueInFolderIndex } from './folder' ; console.log(valueInFile); console.log(valueInFolderIndex); |
Here is loaders-test/commonjs-extension-resolution-loader/test/basic-fixtures/file.js:
export const valueInFile = ‘hello from file.js’;
Here is loaders-test/commonjs-extension-resolution-loader/test/basic-fixtures/folder/index.js:
export const valueInFolderIndex = ‘hello from folder/index.js’;
We have mentioned in another article that there are two ways to execute ESM code:
Regardless, the following two commands will fail.
% node test/basic-fixtures/index % node test/basic-fixtures/index.js
However, all these issues can be resolved by the custom loader, loaders-test/commonjs-extension-resolution-loader/loader.js:
import { isBuiltin } from 'node:module' ; import { dirname } from 'node:path' ; import { cwd } from 'node:process' ; import { fileURLToPath, pathToFileURL } from 'node:url' ; import { promisify } from 'node:util' ; import resolveCallback from 'resolve/async.js' ; const resolveAsync = promisify(resolveCallback); const baseURL = pathToFileURL(cwd() + '/' ).href; export async function resolve(specifier, context, next) { const { parentURL = baseURL } = context; if (isBuiltin(specifier)) { return next(specifier, context); } // `resolveAsync` works with paths, not URLs if (specifier.startsWith( 'file://' )) { specifier = fileURLToPath(specifier); } const parentPath = fileURLToPath(parentURL); let url; try { const resolution = await resolveAsync(specifier, { basedir: dirname(parentPath), // For whatever reason, --experimental-specifier-resolution=node doesn't search for .mjs extensions // but it does search for index.mjs files within directories extensions: [ '.js' , '.json' , '.node' , '.mjs' ], }); url = pathToFileURL(resolution).href; } catch (error) { if (error.code === 'MODULE_NOT_FOUND' ) { // Match Node's error code error.code = 'ERR_MODULE_NOT_FOUND' ; } throw error; } return next(url, context); } |
With the loader, the above failed commands work well:
% node --loader=./loader.js test/basic-fixtures/index (node:56149) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time (Use `node --trace-warnings ...` to show where the warning was created) hello from file.js hello from folder/index.js % node --loader=./loader.js test/basic-fixtures/index.js (node:56160) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time (Use `node --trace-warnings ...` to show where the warning was created) hello from file.js hello from folder/index.js |
With custom loaders, there is no need for the –experimental-specifier-resolution flag.
For the following two reasons, Node.js has dropped the support for DTrace/SystemTap/ETW:
Node.js 19 has updated V8 JavaScript engine to V8 10.7, which includes a new function, Intl.NumberFormat, for language-sensitive number formatting.
Intl.NumberFormat(locales, options)
locales is an optional parameter, which is a BCP 47 language tag, or an array of such strings. Here is the BCP 47 language tag list:
options is also an optional parameter, which is an object with some or all of these properties: compactDisplay, currency, currencyDisplay, currencySign, localeMatcher, notation, numberingSystem, signDisplay, style, unit, unitDisplay, useGrouping, roundingMode, roundingPriority, roundingIncrement, trailingZeroDisplay, minimumIntegerDigits, minimumFractionDigits, maximumFractionDigits, minimumSignificantDigits, and maximumSignificantDigits.
Among them, style chooses the formatting style, with the following supported values:
If style is set to ‘currency’, the currency property is required. currency takes ISO 4217 currency code that is listed in this table. By default, minimumFractionDigits and maximumFractionDigits are both set to 2.
Let’s take a look at this example:
const number = 123456.789; console.log( new Intl.NumberFormat( 'de-DE' , { style: 'currency' , currency: 'EUR' }).format(number)); console.log( new Intl.NumberFormat( 'ja-JP' , { style: 'currency' , currency: 'JPY' }).format(number)); console.log( new Intl.NumberFormat( 'ar-SA' , { style: 'currency' , currency: 'EGP' }).format(number)); console.log( new Intl.NumberFormat( 'zh-CN' , { style: 'currency' , currency: 'CNY' }).format(number)); |
For more information and to develop web applications using Node JS, Hire Node Developer from us as we give you a high-quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft”.
To develop your custom web app using Node JS, please visit our technology page.
Content Source:
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
© 2024 — HK Infosoft. All Rights Reserved.
© 2024 — HK Infosoft. All Rights Reserved.
T&C | Privacy Policy | Sitemap