-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathindex.mdx
133 lines (106 loc) · 3.56 KB
/
index.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: Run JavaScript Everywhere
layout: home
---
<section>
<WithBadge section="index" />
<div>
<h1 className="special">Run JavaScript Everywhere</h1>
Node.js® is a free, open-source, cross-platform JavaScript runtime environment
that lets developers create servers, web apps, command line tools and scripts.
</div>
<div>
<WithNodeRelease status={['LTS']}>
{({ release }) => (
<>
<DownloadButton release={release}>Download Node.js (LTS)</DownloadButton>
<small>
Downloads Node.js <b>{release.versionWithPrefix}</b>
<sup title="Downloads a Node.js installer for your current platform">1</sup> with long-term support.
Node.js can also be installed via <Link href="/download">version managers</Link>.
</small>
</>
)}
</WithNodeRelease>
<WithNodeRelease status={['Current']}>
{({ release }) => (
<small>
Want new features sooner?
Get <b>Node.js <DownloadLink release={release}>{release.versionWithPrefix}</DownloadLink></b>
<sup title="Downloads a Node.js installer for your current platform">1</sup> instead.
</small>
)}
</WithNodeRelease>
</div>
</section>
<section>
<div>
```js displayName="Create an HTTP Server"
// server.mjs
import { createServer } from 'node:http';
const server = createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World!\n');
});
// starts a simple http server locally on port 3000
server.listen(3000, '127.0.0.1', () => {
console.log('Listening on 127.0.0.1:3000');
});
// run with `node server.mjs`
```
```js displayName="Write Tests"
// tests.mjs
import assert from 'node:assert';
import test from 'node:test';
test('that 1 is equal 1', () => {
assert.strictEqual(1, 1);
});
test('that throws as 1 is not equal 2', () => {
// throws an exception because 1 != 2
assert.strictEqual(1, 2);
});
// run with `node tests.mjs`
```
```js displayName="Read and Hash a File"
// crypto.mjs
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
const hasher = createHash('sha1');
hasher.setEncoding('hex');
// ensure you have a `package.json` file for this test!
hasher.write(await readFile('package.json'));
hasher.end();
const fileHash = hasher.read();
// run with `node crypto.mjs`
```
```js displayName="Streams Pipeline"
// streams.mjs
import { pipeline } from 'node:stream/promises';
import { createReadStream, createWriteStream } from 'node:fs';
import { createGzip } from 'node:zlib';
// ensure you have a `package.json` file for this test!
await pipeline
(
createReadStream('package.json'),
createGzip(),
createWriteStream('package.json.gz')
);
// run with `node streams.mjs`
```
```js displayName="Work with Threads"
// threads.mjs
import { Worker, isMainThread,
workerData, parentPort } from 'node:worker_threads';
if (isMainThread) {
const data = 'some data';
const worker = new Worker(import.meta.filename, { workerData: data });
worker.on('message', msg => console.log('Reply from Thread:', msg));
} else {
const source = workerData;
parentPort.postMessage(btoa(source.toUpperCase()));
}
// run with `node threads.mjs`
```
</div>
Learn more what Node.js is able to offer with our [Learning materials](/learn).
</section>