+16
-11
pages/en/index.mdx
+16
-11
pages/en/index.mdx
···
43
43
<section>
44
44
<div>
45
45
```js displayName="Create an HTTP Server"
46
+
// make a file `server.mjs` and run with `node server.mjs`
46
47
import { createServer } from 'node:http';
47
48
48
49
const server = createServer((req, res) => {
···
57
58
```
58
59
59
60
```js displayName="Write Tests"
61
+
// make a file `tests.mjs` and run with `node tests.mjs`
60
62
import assert from 'node:assert';
61
63
import test from 'node:test';
62
64
···
71
73
```
72
74
73
75
```js displayName="Read and Hash a File"
76
+
// make a file `crypto.mjs` and run with `node crypto.mjs`
74
77
import { createHash } from 'node:crypto';
75
78
import { readFile } from 'node:fs/promises';
76
79
77
80
const hasher = createHash('sha1');
78
-
const fileContent = await readFile('./package.json');
79
81
80
82
hasher.setEncoding('hex');
81
-
hasher.write(fileContent);
83
+
// ensure you have a `package.json` file for this test!
84
+
hasher.write(await readFile('package.json'));
82
85
hasher.end();
83
86
84
87
const fileHash = hasher.read();
85
88
```
86
89
87
90
```js displayName="Read Streams"
91
+
// make a file `streams.mjs` and run with `node streams.mjs`
92
+
import { pipeline } from 'node:stream';
88
93
import { createReadStream, createWriteStream } from 'node:fs';
94
+
import { createGzip } from 'node:zlib';
95
+
import { promisify } from 'node:util';
89
96
90
-
const res = await fetch('https://nodejs.org/dist/index.json');
91
-
const json = await res.json(); // yields a json object
92
-
93
-
const readableStream = createReadStream('./package.json');
94
-
const writableStream = createWriteStream('./package2.json');
95
-
96
-
readableStream.setEncoding('utf8');
97
-
98
-
readableStream.on('data', chunk => writableStream.write(chunk));
97
+
await promisify(pipeline)
98
+
( // ensure you have a `package.json` file for this test!
99
+
createReadStream('package.json'),
100
+
createGzip(),
101
+
createWriteStream('package.json')
102
+
);
99
103
```
100
104
101
105
```js displayName="Work with Threads"
106
+
// make a file `threads.mjs` and run with `node threads.mjs`
102
107
import { Worker, isMainThread,
103
108
workerData, parentPort } from 'node:worker_threads';
104
109