|
| 1 | +--- |
| 2 | +title: Exécuter du JavaScript partout |
| 3 | +layout: home |
| 4 | +--- |
| 5 | + |
| 6 | +<section> |
| 7 | + <WithBadgeGroup section="index" /> |
| 8 | + |
| 9 | + <div> |
| 10 | + <h1 className="special">Exécuter du JavaScript partout</h1> |
| 11 | + |
| 12 | + Node.js® est un environnement d'exécution JavaScript gratuit, open-source et multiplateforme qui permet aux développeurs de créer des serveurs, des applications web, des outils en ligne de commande et des scripts. |
| 13 | + |
| 14 | + </div> |
| 15 | + |
| 16 | + <div className="flex gap-4"> |
| 17 | + <div className="flex flex-col gap-2"> |
| 18 | + <Button kind="special" className="!hidden dark:!block" href="/download">Obtenir Node.js®</Button> |
| 19 | + |
| 20 | + <Button kind="primary" className="!block dark:!hidden" href="/download">Obtenir Node.js®</Button> |
| 21 | + |
| 22 | + <Button kind="secondary" className="!block" href="/blog/announcements/node-18-eol-support"> |
| 23 | + <span>Obtenir une aide à la sécurité</span> |
| 24 | + |
| 25 | + <br /> |
| 26 | + |
| 27 | + <small className="!text-xs">pour Node.js 18 et moins</small> |
| 28 | + </Button> |
| 29 | + </div> |
| 30 | + |
| 31 | + </div> |
| 32 | +</section> |
| 33 | + |
| 34 | +<section> |
| 35 | + <div> |
| 36 | + ```js displayName="Create an HTTP Server" |
| 37 | + // server.mjs |
| 38 | + import { createServer } from 'node:http'; |
| 39 | + |
| 40 | + const server = createServer((req, res) => { |
| 41 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); |
| 42 | + res.end('Hello World!\n'); |
| 43 | + }); |
| 44 | + |
| 45 | + // starts a simple http server locally on port 3000 |
| 46 | + server.listen(3000, '127.0.0.1', () => { |
| 47 | + console.log('Listening on 127.0.0.1:3000'); |
| 48 | + }); |
| 49 | + |
| 50 | + // run with `node server.mjs` |
| 51 | + ``` |
| 52 | + |
| 53 | + ```js displayName="Write Tests" |
| 54 | + // tests.mjs |
| 55 | + import assert from 'node:assert'; |
| 56 | + import test from 'node:test'; |
| 57 | + |
| 58 | + test('that 1 is equal 1', () => { |
| 59 | + assert.strictEqual(1, 1); |
| 60 | + }); |
| 61 | + |
| 62 | + test('that throws as 1 is not equal 2', () => { |
| 63 | + // throws an exception because 1 != 2 |
| 64 | + assert.strictEqual(1, 2); |
| 65 | + }); |
| 66 | + |
| 67 | + // run with `node tests.mjs` |
| 68 | + ``` |
| 69 | + |
| 70 | + ```js displayName="Read and Hash a File" |
| 71 | + // crypto.mjs |
| 72 | + import { createHash } from 'node:crypto'; |
| 73 | + import { readFile } from 'node:fs/promises'; |
| 74 | + |
| 75 | + const hasher = createHash('sha1'); |
| 76 | + |
| 77 | + hasher.setEncoding('hex'); |
| 78 | + // ensure you have a `package.json` file for this test! |
| 79 | + hasher.write(await readFile('package.json')); |
| 80 | + hasher.end(); |
| 81 | + |
| 82 | + const fileHash = hasher.read(); |
| 83 | + |
| 84 | + // run with `node crypto.mjs` |
| 85 | + ``` |
| 86 | + |
| 87 | + ```js displayName="Streams Pipeline" |
| 88 | + // streams.mjs |
| 89 | + import { createReadStream, createWriteStream } from 'node:fs'; |
| 90 | + import { pipeline } from 'node:stream/promises'; |
| 91 | + import { createGzip } from 'node:zlib'; |
| 92 | + |
| 93 | + // ensure you have a `package.json` file for this test! |
| 94 | + await pipeline( |
| 95 | + createReadStream('package.json'), |
| 96 | + createGzip(), |
| 97 | + createWriteStream('package.json.gz') |
| 98 | + ); |
| 99 | + |
| 100 | + // run with `node streams.mjs` |
| 101 | + ``` |
| 102 | + |
| 103 | + ```js displayName="Work with Threads" |
| 104 | + // threads.mjs |
| 105 | + import { Worker, isMainThread, |
| 106 | + workerData, parentPort } from 'node:worker_threads'; |
| 107 | + |
| 108 | + if (isMainThread) { |
| 109 | + const data = 'some data'; |
| 110 | + const worker = new Worker(import.meta.filename, { workerData: data }); |
| 111 | + worker.on('message', msg => console.log('Reply from Thread:', msg)); |
| 112 | + } else { |
| 113 | + const source = workerData; |
| 114 | + parentPort.postMessage(btoa(source.toUpperCase())); |
| 115 | + } |
| 116 | + |
| 117 | + // run with `node threads.mjs` |
| 118 | + ``` |
| 119 | +
|
| 120 | + </div> |
| 121 | +
|
| 122 | +Apprenez-en plus sur ce que Node.js est capable d'offrir avec notre [Matériel d'apprentissage](/learn). |
| 123 | +
|
| 124 | +</section> |
0 commit comments