diff --git a/pages/vi/index.mdx b/pages/vi/index.mdx new file mode 100644 index 0000000000000..036cd598f0925 --- /dev/null +++ b/pages/vi/index.mdx @@ -0,0 +1,121 @@ +--- +title: Run JavaScript Everywhere +layout: home.hbs +--- + +
+ + +
+

Run JavaScript Everywhere

+ +``` +Node.jsĀ® is a free, open-source, cross-platform JavaScript runtime +environment that lets developers create servers, web apps, +command line tools and scripts. +``` + +
+ +
+ + {({ release }) => ( + <> + Download Node.js (LTS) + + Downloads Node.js {release.versionWithPrefix} + 1 with long-term support. + Node.js can also be installed via package managers. + + + )} + + + {({ release }) => ( + + Want new features sooner? + Get Node.js {release.versionWithPrefix} + 1 instead. + + )} + +
+
+ +
+
+ ```js displayName="Create an HTTP Server" + 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'); +}); +``` + +```js displayName="Write Tests" +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); +}); +``` + +```js displayName="Read and Hash a File" +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); +hasher.end(); + +const fileHash = hasher.read(); +``` + +```js displayName="Read Streams" +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)); +``` + +```js displayName="Work with Threads" +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())); +} +``` +```` + +
+ Learn more what Node.js is able to offer with our [Learning materials](/learn). +