Rumah >hujung hadapan web >tutorial js >Node.js 19 dikeluarkan secara rasmi, mari bercakap tentang 6 ciri utamanya!
Node 19 telah dikeluarkan secara rasmi Artikel berikut akan memberi anda penjelasan terperinci tentang 6 ciri utama Node.js 19. Saya harap ia akan membantu semua orang!
Diterjemahkan daripada: 6 Ciri Utama Node.js 19. Butiran ciri baharu Node.js 19… oleh Jennifer Fu | |. Pengaturcaraan Lebih Baik
Node.js 14 akan menamatkan penyelenggaraan kemas kini pada April 2023 dan Node.js 16 (LTS) dijangka menamatkan penyelenggaraan kemas kini pada September 2023 .
Dan Node 19 telah dikeluarkan pada 2022-10-18. [Cadangan tutorial berkaitan: tutorial video nodejs]
Kami tahu bahawa terdapat dua versi Node.js: LTS dan Semasa
Antaranya, versi Semasa biasanya dikeluarkan setiap 6 bulan.
Versi bernombor genap baharu dikeluarkan setiap April;
Versi bernombor ganjil baharu dikeluarkan setiap bulan Oktober; terkini Versi pengguna awal "Semasa" membawa sejumlah 6 ciri utama.
1. HTTP(S)/1.1 KeepAlive lalai kepada trueUjian kod:
Kami boleh membandingkan perbezaan konfigurasi Ejen pelayan nod antara v16 dan v19:const http = require('node:http'); console.log(http.globalAgent); const https = require('node:https'); console.log(https.globalAgent);V16
% 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 }Barisan 14, 16, 42 dan 44 menetapkan nilai lalai dan masa keepAlive Mendayakan keepAlive boleh menggunakan semula sambungan dan meningkatkan daya pemprosesan rangkaian.
% 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 }
Pengubahsuaian ini mengoptimumkan lagi pengalaman dan prestasi.
2. API WebCrypto Stabilclose()
http(s).Server.close
API WebCrypto ialah antara muka sistem yang dibina menggunakan kriptografi, yang cenderung stabil dalam node.js v19 (kecuali Ed25519, Ed448, Kecuali X25519 dan X448).
Kami boleh mengaksesnya dengan memanggil
atauglobalThis.crypto
3 Pelarasan resolusi ESM tersuairequire('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); })();, yang fungsinya kini tersedia melalui pemuat tersuai. boleh diuji dalam perpustakaan ini:
nodejs/loaders-test: Contoh yang menunjukkan API Node.js ECMAScript Modules LoadersI love cupcakes
fail:
% 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 }
--experimental-specifier-resolution
Selepas menetapkan pemuat tersuai, maka Boleh menyelesaikan masalah di atas:
git clone https://github.com/nodejs/loaders-test.git % cd loaders-test/commonjs-extension-resolution-loader % yarn installtidak akan lagi melaporkan ralat dan berjalan seperti biasa.
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
Dalam Node.js v19, sokongan untuk DTrace/SystemTap/ETW telah dialih keluar, terutamanya kerana keutamaan sumber isu. ./file.js
./file.mjs
Data menunjukkan bahawa beberapa orang menggunakan DTrace, SystemTap atau ETW, dan tidak ada gunanya mengekalkannya.
Jika anda ingin menyambung semula penggunaan, anda boleh menimbulkan isu =>
github.com/nodejs/node…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.js5
Nod peningkatan masa - -pilihan jam tangan.
在 "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.js 14 将在 2023 年 4 月结束更新维护,Node.js 16 (LTS) 预计将在 2023 年 9 月结束更新维护。
建议大家开始计划将版本按需升级到 Node.js 16(LTS)或 Node.js 18(LTS)。
更多node相关知识,请访问:nodejs 教程!
Atas ialah kandungan terperinci Node.js 19 dikeluarkan secara rasmi, mari bercakap tentang 6 ciri utamanya!. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!