Table of Contents
1. HTTP(S)/1.1 KeepAlive defaults to true
2. Stable WebCrypto API
3. Custom ESM resolution adjustment
4. Removed support for DTrace/SystemTap/ETW
5. Upgrade the V8 engine to 10.7
6. Experiment with Node watch mode
Home Web Front-end JS Tutorial Node.js 19 is officially released, let's talk about its 6 major features!

Node.js 19 is officially released, let's talk about its 6 major features!

Nov 16, 2022 pm 08:34 PM
javascript front end node.js

Node 19 has been officially released. The following article will give you a detailed explanation of the 6 major features of Node.js 19. I hope it will be helpful to everyone!

Node.js 19 is officially released, let's talk about its 6 major features!

Translated from: 6 Major Features of Node.js 19. Details of Node.js 19 new features… | by Jennifer Fu | Oct, 2022 | Better Programming


Node.js 14 will end update maintenance in April 2023, Node.js 16 (LTS) is expected to end update maintenance in September 2023 .

And Node 19 was released on 2022-10-18. [Related tutorial recommendations: nodejs video tutorial]

We know that there are two versions of Node.js: LTS and Current

Node.js 19 is officially released, lets talk about its 6 major features!

Among them , the Current version is usually released every 6 months.

New even-numbered versions are released every April;

New odd-numbered versions are released every October;

In the past October, the released V19.0.1 became the latest The "Current" early adopter version brings a total of 6 major features.

1. HTTP(S)/1.1 KeepAlive defaults to true

Node.js v19 sets the keepAlive default value to true, which means that all outbound HTTP( s) All connections will use HTTP 1.1 keepAlive, and the default time is 5S;

Code test:

1

2

3

4

const http = require('node:http');

console.log(http.globalAgent);

const https = require('node:https');

console.log(https.globalAgent);

Copy after login

We can compare the node server Agent configuration differences between v16 and v19:

  • V16

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

% 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

}

Copy after login

Lines 18 and 40, keepAlive is set to false by default;

  • V19

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

% 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

}

Copy after login

Line 14 , Lines 16, 42, and 44 set the keepAlive default value and time;

Enabling keepAlive can reuse connections and improve network throughput.

In addition, the server will automatically disconnect the idle client when calling close(), which is implemented internally by relying on the http(s).Server.close API;

These modifications further optimize the experience and performance.

2. Stable WebCrypto API

WebCrypto API is a system interface built using cryptography, which tends to be stable in node.js v19 (except Ed25519, Ed448, Except X25519 and X448).

We can access it by calling globalThis.crypto or require('node:crypto').webcrypto, the following is the subtle encryption function For example;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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);

 

})();

Copy after login

First generate the HMAC key, and the generated key can be used to verify the integrity and authenticity of the message data;

Then, for the string I love cupcakes Encryption;

Finally create a message digest, which is an encrypted hash function;

Display on the console: key, message, digest information

1

2

3

4

5

6

7

8

9

10

11

12

% 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

}

Copy after login

3. Custom ESM resolution adjustment

Node.js has been removed --experimental-specifier-resolution , and its functionality can now be achieved through a custom loader.

Can be tested in this library: nodejs/loaders-test: Examples demonstrating the Node.js ECMAScript Modules Loaders API

1

2

3

4

5

git clone https://github.com/nodejs/loaders-test.git

 

% cd loaders-test/commonjs-extension-resolution-loader

 

% yarn install

Copy after login

For exampleloaders-test/ commonjs-extension-resolution-loader/test/basic-fixtures/index.js File:

1

2

3

4

5

6

7

import { version } from &#39;process&#39;;

 

import { valueInFile } from &#39;./file&#39;;

import { valueInFolderIndex } from &#39;./folder&#39;;

 

console.log(valueInFile);

console.log(valueInFolderIndex);

Copy after login

./file If there is no custom loader, the file will not be found extension, such as ./file.js or ./file.mjs

After setting a custom loader, the above problem can be solved:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

import { isBuiltin } from &#39;node:module&#39;;

import { dirname } from &#39;node:path&#39;;

import { cwd } from &#39;node:process&#39;;

import { fileURLToPath, pathToFileURL } from &#39;node:url&#39;;

import { promisify } from &#39;node:util&#39;;

 

import resolveCallback from &#39;resolve/async.js&#39;;

 

const resolveAsync = promisify(resolveCallback);

 

const baseURL = pathToFileURL(cwd() + &#39;/&#39;).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(&#39;file://&#39;)) {

    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&#39;t search for .mjs extensions

      // but it does search for index.mjs files within directories

      extensions: [&#39;.js&#39;, &#39;.json&#39;, &#39;.node&#39;, &#39;.mjs&#39;],

    });

    url = pathToFileURL(resolution).href;

  } catch (error) {

    if (error.code === &#39;MODULE_NOT_FOUND&#39;) {

      // Match Node&#39;s error code

      error.code = &#39;ERR_MODULE_NOT_FOUND&#39;;

    }

    throw error;

  }

 

  return next(url, context);

}

Copy after login

Test command:

1

2

3

4

% 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

Copy after login

will no longer report errors and run normally.

4. Removed support for DTrace/SystemTap/ETW

In Node.js v19, support for DTrace/SystemTap/ETW has been removed, mainly Because of resource priority issues.

The data shows that few people use DTrace, SystemTap or ETW, and there is not much point in maintaining them.

If you want to resume use, you can file issues => github.com/nodejs/node…

5. Upgrade the V8 engine to 10.7

Node.js v19 updates the V8 JavaScript engine to V8 10.7, which includes a new function Intl.NumberFormat for formatting sensitive numbers.

1

Intl.NumberFormat(locales, options)

Copy after login

For different languages, pass in different locales:

1

2

3

4

5

6

const number = 123456.789;

 

console.log(new Intl.NumberFormat(&#39;de-DE&#39;, { style: &#39;currency&#39;, currency: &#39;EUR&#39; }).format(number));

console.log(new Intl.NumberFormat(&#39;ja-JP&#39;, { style: &#39;currency&#39;, currency: &#39;JPY&#39; }).format(number));

console.log(new Intl.NumberFormat(&#39;ar-SA&#39;, { style: &#39;currency&#39;, currency: &#39;EGP&#39; }).format(number));

console.log(new Intl.NumberFormat(&#39;zh-CN&#39;, { style: &#39;currency&#39;, currency: &#39;CNY&#39; }).format(number));

Copy after login

6. Experiment with Node watch mode

Added node during runtime -- watch option.

在 "watch" 模式下运行,当导入的文件被改变时,会重新启动进程。

比如:

1

2

3

4

5

6

7

8

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")

);

Copy after login

1

2

3

4

% 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

Copy after login

Node.js 14 将在 2023 年 4 月结束更新维护,Node.js 16 (LTS) 预计将在 2023 年 9 月结束更新维护。

建议大家开始计划将版本按需升级到 Node.js 16(LTS)或 Node.js 18(LTS)。

更多node相关知识,请访问:nodejs 教程

The above is the detailed content of Node.js 19 is officially released, let's talk about its 6 major features!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Vue: a perfect pairing of front-end development tools PHP and Vue: a perfect pairing of front-end development tools Mar 16, 2024 pm 12:09 PM

PHP and Vue: a perfect pairing of front-end development tools. In today's era of rapid development of the Internet, front-end development has become increasingly important. As users have higher and higher requirements for the experience of websites and applications, front-end developers need to use more efficient and flexible tools to create responsive and interactive interfaces. As two important technologies in the field of front-end development, PHP and Vue.js can be regarded as perfect tools when paired together. This article will explore the combination of PHP and Vue, as well as detailed code examples to help readers better understand and apply these two

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

Is Django front-end or back-end? check it out! Is Django front-end or back-end? check it out! Jan 19, 2024 am 08:37 AM

Django is a web application framework written in Python that emphasizes rapid development and clean methods. Although Django is a web framework, to answer the question whether Django is a front-end or a back-end, you need to have a deep understanding of the concepts of front-end and back-end. The front end refers to the interface that users directly interact with, and the back end refers to server-side programs. They interact with data through the HTTP protocol. When the front-end and back-end are separated, the front-end and back-end programs can be developed independently to implement business logic and interactive effects respectively, and data exchange.

Exploring Go language front-end technology: a new vision for front-end development Exploring Go language front-end technology: a new vision for front-end development Mar 28, 2024 pm 01:06 PM

As a fast and efficient programming language, Go language is widely popular in the field of back-end development. However, few people associate Go language with front-end development. In fact, using Go language for front-end development can not only improve efficiency, but also bring new horizons to developers. This article will explore the possibility of using the Go language for front-end development and provide specific code examples to help readers better understand this area. In traditional front-end development, JavaScript, HTML, and CSS are often used to build user interfaces

Django: A magical framework that can handle both front-end and back-end development! Django: A magical framework that can handle both front-end and back-end development! Jan 19, 2024 am 08:52 AM

Django: A magical framework that can handle both front-end and back-end development! Django is an efficient and scalable web application framework. It is able to support multiple web development models, including MVC and MTV, and can easily develop high-quality web applications. Django not only supports back-end development, but can also quickly build front-end interfaces and achieve flexible view display through template language. Django combines front-end development and back-end development into a seamless integration, so developers don’t have to specialize in learning

Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Combination of Golang and front-end technology: explore how Golang plays a role in the front-end field Mar 19, 2024 pm 06:15 PM

Combination of Golang and front-end technology: To explore how Golang plays a role in the front-end field, specific code examples are needed. With the rapid development of the Internet and mobile applications, front-end technology has become increasingly important. In this field, Golang, as a powerful back-end programming language, can also play an important role. This article will explore how Golang is combined with front-end technology and demonstrate its potential in the front-end field through specific code examples. The role of Golang in the front-end field is as an efficient, concise and easy-to-learn

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

See all articles