Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better examples on home #6457

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions pages/en/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ layout: home
<section>
<div>
```js displayName="Create an HTTP Server"
// make a file `server.mjs` and run with `node server.mjs`
import { createServer } from 'node:http';

const server = createServer((req, res) => {
Expand All @@ -57,6 +58,7 @@ layout: home
```

```js displayName="Write Tests"
// make a file `tests.mjs` and run with `node tests.mjs`
import assert from 'node:assert';
import test from 'node:test';

Expand All @@ -71,34 +73,37 @@ layout: home
```

```js displayName="Read and Hash a File"
// make a file `crypto.mjs` and run with `node crypto.mjs`
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';

const hasher = createHash('sha1');
const fileContent = await readFile('./package.json');

hasher.setEncoding('hex');
hasher.write(fileContent);
// ensure you have a `package.json` file for this test!
hasher.write(await readFile('./package.json'));
bmuenzenmeyer marked this conversation as resolved.
Show resolved Hide resolved
hasher.end();

const fileHash = hasher.read();
bmuenzenmeyer marked this conversation as resolved.
Show resolved Hide resolved
```

```js displayName="Read Streams"
// make a file `streams.mjs` and run with `node streams.mjs`
import { pipeline } from 'node:stream';
import { createReadStream, createWriteStream } from 'node:fs';

const res = await fetch('https://nodejs.org/dist/index.json');
const json = await res.json(); // yields a json object

const readableStream = createReadStream('./package.json');
const writableStream = createWriteStream('./package2.json');

readableStream.setEncoding('utf8');

readableStream.on('data', chunk => writableStream.write(chunk));
import { createGzip } from 'node:zlib';
import { promisify } from 'node:util';

await promisify(pipeline)
( // ensure you have a `package.json` file for this test!
createReadStream('package.json'),
createGzip(),
createWriteStream('package.json')
ovflowd marked this conversation as resolved.
Show resolved Hide resolved
);
```

```js displayName="Work with Threads"
// make a file `threads.mjs` and run with `node threads.mjs`
import { Worker, isMainThread,
workerData, parentPort } from 'node:worker_threads';

Expand Down
Loading