iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Node.js19有哪些特性
  • 629
分享到

Node.js19有哪些特性

2023-07-04 15:07:54 629人浏览 泡泡鱼
摘要

今天小编给大家分享一下node.js19有哪些特性的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。node.js 14 将在

今天小编给大家分享一下node.js19有哪些特性的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

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

而Node 19 在 2022-10-18 发布。

我们知道 Node.js 版本分两种:LTS 和 Current

Node.js19有哪些特性

其中,Current 版本通常每 6 个月发布一次。

每年 4 月份发布新的偶数版本;

每年 10 月份发布新的奇数版本;

在刚过去的 10 月,发布的 V19.0.1 成为最新的 “Current” 尝鲜版,它一共带来 6 大特性。

1. HTTP(S)/1.1 KeepAlive 默认为 true

Node.js v19 设置 keepAlive 默认值为 true,这意味着所有出站的 Http(s) 连接都将使用 HTTP 1.1 keepAlive,默认时间为 5S;

代码测试

const http = require('node:http');console.log(http.globalAgent);const https = require('node:https');console.log(https.globalAgent);

我们可以对比看看 v16 和 v19 的 node server Agent 配置差异:

  • V16

% nvm use 16Now using node v16.0.0 (npm v7.10.0)% node serverAgent {  _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}

第 18、40 行,keepAlive 默认设置为 false;

  • V19

% nvm use 19Now using node v19.0.0 (npm v8.19.2)% node serverAgent {  _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}

第 14、16、42、44 行设置 keepAlive 默认值及时间;

启用 keepAlive 能使连接重用,提高网络的吞吐量。

另外,服务器将在调用 close() 自动断开空闲的客户端,内部依靠 http(s).Server.close api 实现;

这些修改,进一步优化了体验和性能。

2. 稳定的 WebCrypto API

WEBCrypto API 是一个使用密码学构建的系统接口,在 node.js v19 趋于稳定(除 Ed25519、Ed448、X25519、X448 外)。

我们可以通过调用 globalThis.cryptorequire('node:crypto').webcrypto 来访问,下面以 subtle 加密函数为例;

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

首先生成 HMAC 密钥,生成的密钥可同时用于验证消息数据完整性和真实性;

然后,对字符串 I love cupcakes 加密;

最后创建 消息摘要,它是一种加密散列函数;

在控制台显示:key 、message 、digest 信息

% node serverkey = 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}

3. 自定义 ESM resolution 调整

Node.js 已经删除 --experimental-specifier-resolution ,其功能现在可以通过自定义加载器实现。

可以在这个库中测试:nodejs/loaders-test: Examples demonstrating the Node.js ECMAScript Modules Loaders API

git clone https://GitHub.com/nodejs/loaders-test.git% cd loaders-test/commonjs-extension-resolution-loader% yarn install

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

./file 如果没有自定义加载器,不会去查找文件的扩展名,比如 ./file.js./file.mjs

设置自定义加载器后,则可解决上述问题:

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

测试命令:

% 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

将不会再报错,正常运行。

4. 移除对 DTrace/SystemTap/ETW 支持

在 Node.js v19中,移除了对 DTrace/SystemTap/ETW 的支持,主要是因为资源的优先级问题。

数据表明很少人用到 DTrace、SystemTap 或 ETW,维护它们没有多大的意义。

如果你想恢复使用,可提 issues => github.com/nodejs/node…

5. 升级 V8 引擎至 10.7

Node.js v19 将 V8 javascript 引擎更新至 V8 10.7,其中包含一个新函数 Intl.NumberFORMat,用于格式化敏感数字。

Intl.NumberFormat(locales, options)

对于不同的语言,传入不同的 locales:

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

6. 试验 Node watch 模式

运行时增加了 node --watch 选项。

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

比如:

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"));
% 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.js19有哪些特性”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网精选频道。

--结束END--

本文标题: Node.js19有哪些特性

本文链接: https://www.lsjlt.com/news/345908.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Node.js19有哪些特性
    今天小编给大家分享一下Node.js19有哪些特性的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。Node.js 14 将在 ...
    99+
    2023-07-04
  • MySQL特性有哪些
    这篇文章主要介绍“MySQL特性有哪些”,在日常操作中,相信很多人在MySQL特性有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL特性有哪些”的疑惑有所帮助!接...
    99+
    2024-04-02
  • VB.NET有哪些特性
    这篇文章将为大家详细讲解有关VB.NET有哪些特性,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。下面以VB2010为例加以说明。首先,在语言中取消连接符这个改动表面看很小,因为basic的开发者都知晓,原...
    99+
    2023-06-17
  • Flexbuilder4特性有哪些
    这篇文章主要介绍Flexbuilder4特性有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Flexbuilder4的10个新特性MAX大会上HeidiWilliams用10分钟介绍了FlexBuilder4(G...
    99+
    2023-06-17
  • HASH有哪些特性
    这篇文章主要为大家展示了“HASH有哪些特性”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“HASH有哪些特性”这篇文章吧。哈希满足特性平衡性 :平衡性是指哈希的...
    99+
    2024-04-02
  • Node.js有哪些特性
    这篇文章主要讲解了“Node.js有哪些特性”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Node.js有哪些特性”吧!为何用Node对于我来说,对于团队来...
    99+
    2024-04-02
  • Django特性有哪些
    这篇文章主要介绍了Django特性有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。在正式开始学习Django框架前,我们不妨先来认识一下它的特性。我们可以简单的把特性分为...
    99+
    2023-06-14
  • FlexBuilder有哪些特性
    这篇文章主要为大家展示了“FlexBuilder有哪些特性”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“FlexBuilder有哪些特性”这篇文章吧。【FlexBuilder】FlexBuild...
    99+
    2023-06-17
  • graphql有哪些特性
    这篇文章主要介绍“graphql有哪些特性”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“graphql有哪些特性”文章能帮助大家解决问题。graphql 是一种用于 API 的查询语言,对你的 AP...
    99+
    2023-06-27
  • thinkphp有哪些特性
    本篇内容主要讲解“thinkphp有哪些特性”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“thinkphp有哪些特性”吧!thinkphp是一个免费开源的、快速的、简单的、面向对象的轻量级PHP...
    99+
    2023-06-29
  • Flutter有哪些特性
    这篇文章主要介绍“Flutter有哪些特性”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Flutter有哪些特性”文章能帮助大家解决问题。一、Flutter是什么Flutter是 Google 于 ...
    99+
    2023-06-27
  • ecmascript5有哪些特性
    本篇内容主要讲解“ecmascript5有哪些特性”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ecmascript5有哪些特性”吧! ...
    99+
    2024-04-02
  • angularjs有哪些特性
    本文小编为大家详细介绍“angularjs有哪些特性”,内容详细,步骤清晰,细节处理妥当,希望这篇“angularjs有哪些特性”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。 ...
    99+
    2024-04-02
  • vue3.0的特性有哪些
    vue3.0的特性有哪些?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。vue是什么软件Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它...
    99+
    2023-06-06
  • Linux7的特性有哪些
    Linux7的特性有哪些?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。1.对Windows 产品的支持早在2009年,微软和红帽签订了一份旨在为Windows和Linux用户提...
    99+
    2023-06-07
  • JDK1.5有哪些新特性
    本篇内容主要讲解“JDK1.5有哪些新特性”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“JDK1.5有哪些新特性”吧!  1.泛型(Generic)  C++通过模板技术可以指定集合的元素类型,...
    99+
    2023-06-03
  • Spring的特性有哪些
    Spring的特性有哪些?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Spring引入传统的基于mvc的项目框架结构:Entity / dao / servic...
    99+
    2023-05-31
    spring
  • ES6中有哪些特性
    这篇文章给大家分享的是有关ES6中有哪些特性的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1.Object Shorthand新的对象声明方法允许我们可以不声明对象的 key :...
    99+
    2024-04-02
  • css3新特性有哪些
    这篇文章将为大家详细讲解有关css3新特性有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。   CSS3的新特征有:1、圆角效果;2、图形化边界;3、块阴影与文字阴...
    99+
    2024-04-02
  • CSS3有哪些新特性
    这篇文章主要介绍“CSS3有哪些新特性”,在日常操作中,相信很多人在CSS3有哪些新特性问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”CSS3有哪些新特性”的疑惑有所帮助!接...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作