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