|
| 1 | +--- |
| 2 | +title: Ejecuta JavaScript en cualquier parte |
| 3 | +layout: home |
| 4 | +--- |
| 5 | + |
| 6 | +<section> |
| 7 | +<WithBadge section="index" /> |
| 8 | + |
| 9 | +<div> |
| 10 | + <h1 className="special">Ejecuta JavaScript en cualquier parte</h1> |
| 11 | + |
| 12 | +Node.js® es un entorno de ejecución de JavaScript multiplataforma, de código abierto y gratuito que permite a los desarrolladores crear servidores, aplicaciones web, herramientas de línea de comando y scripts. |
| 13 | + |
| 14 | +</div> |
| 15 | + |
| 16 | +<div> |
| 17 | + <WithNodeRelease status={['LTS']}> |
| 18 | + {({ release }) => ( |
| 19 | + <> |
| 20 | + <DownloadButton release={release}>Descargar Node.js (LTS)</DownloadButton> |
| 21 | + <small> |
| 22 | + Descarga Node.js <b>{release.versionWithPrefix}</b> |
| 23 | + <sup title="Downloads a Node.js installer for your current platform">1</sup> con soporte a largo plazo. |
| 24 | + Node.js también puede ser instalado a través de <a href="/download/package-manager">gestores de paquetes</a>. |
| 25 | + </small> |
| 26 | + </> |
| 27 | + )} |
| 28 | + </WithNodeRelease> |
| 29 | + |
| 30 | + <WithNodeRelease status={['Current']}> |
| 31 | + {({ release }) => ( |
| 32 | + <small> |
| 33 | + ¿Quieres nuevas funciones más pronto? |
| 34 | + Consigue<b>Node.js <DownloadLink release={release}>{release.versionWithPrefix}</DownloadLink></b> |
| 35 | + <sup title="Downloads a Node.js installer for your current platform">1</sup> en vez. |
| 36 | + </small> |
| 37 | + )} |
| 38 | + </WithNodeRelease> |
| 39 | +</div> |
| 40 | +</section> |
| 41 | + |
| 42 | +<section> |
| 43 | +<div> |
| 44 | + ```js displayName="Create an HTTP Server" |
| 45 | + // server.mjs |
| 46 | + import { createServer } from 'node:http'; |
| 47 | + |
| 48 | +const server = createServer((req, res) => { |
| 49 | +res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 50 | +res.end('Hello World!\n'); |
| 51 | +}); |
| 52 | + |
| 53 | +// starts a simple http server locally on port 3000 |
| 54 | +server.listen(3000, '127.0.0.1', () => { |
| 55 | +console.log('Listening on 127.0.0.1:3000'); |
| 56 | +}); |
| 57 | + |
| 58 | +// run with `node server.mjs` |
| 59 | + |
| 60 | +```` |
| 61 | + |
| 62 | +```js displayName="Write Tests" |
| 63 | +// tests.mjs |
| 64 | +import assert from 'node:assert'; |
| 65 | +import test from 'node:test'; |
| 66 | +
|
| 67 | +test('that 1 is equal 1', () => { |
| 68 | + assert.strictEqual(1, 1); |
| 69 | +}); |
| 70 | +
|
| 71 | +test('that throws as 1 is not equal 2', () => { |
| 72 | + // throws an exception because 1 != 2 |
| 73 | + assert.strictEqual(1, 2); |
| 74 | +}); |
| 75 | +
|
| 76 | +// run with `node tests.mjs` |
| 77 | +```` |
| 78 | +
|
| 79 | +```js displayName="Read and Hash a File" |
| 80 | +// crypto.mjs |
| 81 | +import { createHash } from 'node:crypto'; |
| 82 | +import { readFile } from 'node:fs/promises'; |
| 83 | + |
| 84 | +const hasher = createHash('sha1'); |
| 85 | + |
| 86 | +hasher.setEncoding('hex'); |
| 87 | +// ensure you have a `package.json` file for this test! |
| 88 | +hasher.write(await readFile('package.json')); |
| 89 | +hasher.end(); |
| 90 | + |
| 91 | +const fileHash = hasher.read(); |
| 92 | + |
| 93 | +// run with `node crypto.mjs` |
| 94 | +``` |
| 95 | + |
| 96 | +```js displayName="Streams Pipeline" |
| 97 | +// streams.mjs |
| 98 | +import { pipeline } from 'node:stream/promises'; |
| 99 | +import { createReadStream, createWriteStream } from 'node:fs'; |
| 100 | +import { createGzip } from 'node:zlib'; |
| 101 | + |
| 102 | +// ensure you have a `package.json` file for this test! |
| 103 | +await pipeline( |
| 104 | + createReadStream('package.json'), |
| 105 | + createGzip(), |
| 106 | + createWriteStream('package.json.gz') |
| 107 | +); |
| 108 | + |
| 109 | +// run with `node streams.mjs` |
| 110 | +``` |
| 111 | + |
| 112 | +```js displayName="Work with Threads" |
| 113 | +// threads.mjs |
| 114 | +import { |
| 115 | + Worker, |
| 116 | + isMainThread, |
| 117 | + workerData, |
| 118 | + parentPort, |
| 119 | +} from 'node:worker_threads'; |
| 120 | + |
| 121 | +if (isMainThread) { |
| 122 | + const data = 'some data'; |
| 123 | + const worker = new Worker(import.meta.filename, { workerData: data }); |
| 124 | + worker.on('message', msg => console.log('Reply from Thread:', msg)); |
| 125 | +} else { |
| 126 | + const source = workerData; |
| 127 | + parentPort.postMessage(btoa(source.toUpperCase())); |
| 128 | +} |
| 129 | + |
| 130 | +// run with `node threads.mjs` |
| 131 | +``` |
| 132 | +
|
| 133 | +</div> |
| 134 | +
|
| 135 | +Aprenda más sobre lo que Node.js puede ofrecer con nuestros [Materiales de aprendizaje](/learn). |
| 136 | +
|
| 137 | +</section> |
0 commit comments