diff options
author | OneNail <onenail@yeah.net> | 2022-05-03 00:24:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-03 00:24:13 +0300 |
commit | f6f95bf61e84dae44b16c51ebb4a9c5b46194583 (patch) | |
tree | 1f9947a82122e0906dc04863528ec7577f61e2dd /doc | |
parent | ce29d2847b9996602e3b49523d2325aad206543c (diff) |
doc: fix examples in cluster.md
PR-URL: https://github.com/nodejs/node/pull/42889
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Diffstat (limited to 'doc')
-rw-r--r-- | doc/api/cluster.md | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/doc/api/cluster.md b/doc/api/cluster.md index ce38048678c..1f7cfed56fc 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -197,31 +197,35 @@ Similar to the `cluster.on('exit')` event, but specific to this worker. ```mjs import cluster from 'node:cluster'; -const worker = cluster.fork(); -worker.on('exit', (code, signal) => { - if (signal) { - console.log(`worker was killed by signal: ${signal}`); - } else if (code !== 0) { - console.log(`worker exited with error code: ${code}`); - } else { - console.log('worker success!'); - } -}); +if (cluster.isPrimary) { + const worker = cluster.fork(); + worker.on('exit', (code, signal) => { + if (signal) { + console.log(`worker was killed by signal: ${signal}`); + } else if (code !== 0) { + console.log(`worker exited with error code: ${code}`); + } else { + console.log('worker success!'); + } + }); +} ``` ```cjs const cluster = require('node:cluster'); -const worker = cluster.fork(); -worker.on('exit', (code, signal) => { - if (signal) { - console.log(`worker was killed by signal: ${signal}`); - } else if (code !== 0) { - console.log(`worker exited with error code: ${code}`); - } else { - console.log('worker success!'); - } -}); +if (cluster.isPrimary) { + const worker = cluster.fork(); + worker.on('exit', (code, signal) => { + if (signal) { + console.log(`worker was killed by signal: ${signal}`); + } else if (code !== 0) { + console.log(`worker exited with error code: ${code}`); + } else { + console.log('worker success!'); + } + }); +} ``` ### Event: `'listening'` @@ -235,16 +239,12 @@ added: v0.7.0 Similar to the `cluster.on('listening')` event, but specific to this worker. ```mjs -import cluster from 'node:cluster'; - cluster.fork().on('listening', (address) => { // Worker is listening }); ``` ```cjs -const cluster = require('node:cluster'); - cluster.fork().on('listening', (address) => { // Worker is listening }); |