ノードで新しい.JS V 18


ちょうど昨日、Node.js v18は現在の安定版としてリリースされました.ここではいくつかの新機能の迅速なチュートリアルです.

グローバルフェッチ!


ノード.JSは--experimental-fetchフラグの後ろに世界的なフェッチをしました.そして、それはあなたがBrowser Fetch APIをノードでネイティブに使用するのを許すでしょう.jsv 18では、実験的なフェッチAPIはデフォルトで利用可能です.
fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => response.json())
  .then(json => console.log(json))
(node:82823) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{ userId: 1, id: 1, title: 'delectus aut autem', completed: false }
また、 FormData Headers Request 、および Response オブジェクトへのアクセスを取得します.

WebストリーミングAPIへのアクセス


ノード.JSは、現在Web Streaming APIのために実験的支持を持ちます
fetch('https://dev.to/api/articles?per_page=1000&page=1')
    .then(response => response.body)
    .then(rb => rb.getReader())
    .then(reader => {
        const stream = new ReadableStream({
            ...
        })
    })

組み込み試験


ノード.現在、JSはビルトインテストフレームワークを持ちます
import test from 'node:test';
import assert from 'node:assert';

test('true is not false', async t => {
    assert.strictEqual(true, !false);
});
$ node test.js
(node:83584) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
TAP version 13
ok 1 - true is not false
  ---
  duration_ms: 0.000730654
  ...
1..1
# tests 1
# pass 1
# fail 0
# skipped 0
# todo 0
# duration_ms 0.074570679
出力はTAP形式です.あなたはそれをきれいに印刷するimport('node:test')またはtap
$ npm i -g tap
$ tap test.js
index.js 2> (node:84725) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
index.js 2> (Use `node --trace-warnings ...` to show where the warning was created)
 PASS  index.js 1 OK 239.361ms



  🌈 SUMMARY RESULTS 🌈


Suites:   1 passed, 1 of 1 completed
Asserts:  1 passed, of 1
Time:   415.463ms
----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |
----------|---------|----------|---------|---------|-------------------
$ npm i -g faucet
$ node test.js | faucet

(node:84914) ExperimentalWarning: The test runner is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)true is not false
# tests 1
# pass 1
✓ skipped 0
✓ todo 0
✓ duration_ms 0.076367098
あなたはもっと学ぶために

ドキュメント バイナリ!


ユーザーがノードを構築することができます.カスタムv 8のスタートアップのスナップショットは、パフォーマンスを向上させるにはJS.
Laymanの用語では、ノード内の何らかの依存関係をキャッシュすることができます.JSソースコード自体、起動時間を改善する.
$ cd /where/is/node/source/code
$ ./configure --node-snapshot-main=marked.js # where marked.js is the source of the marked library
$ make node
// your-program.js
// globalThis.marked is now deserialized from the snapshot
// so node.js doesnt need to parse it again
// which improves startup time
const marked = globalThis.marked;
marked(/* ... */);
$ out/Release/node your-program.js
ノード.JSはこれのためにJS APIで働いています.配布可能なバイナリとしてJSアプリ!
ノード.JSのV 18はいくつかの本当にエキサイティングな新機能があります.私は永久にフェッチAPIの土地を待っていると私は常にノードを望んできた.jsにはバイナリがありました.テストフレームワークもきちんとしている!