← All notes

Node.js cluster - fork workers and share a server port

Answer: The cluster module can start child processes that all share server ports. Call cluster.fork() from the primary process (cluster.isPrimary). Workers can then listen on the same port.

Examples

import cluster from 'node:cluster';
import http from 'node:http';
import { availableParallelism } from 'node:os';

if (cluster.isPrimary) {
  const n = availableParallelism();
  for (let i = 0; i < n; i++) cluster.fork();
  cluster.on('exit', (worker) => {
    console.error('worker', worker.process.pid, 'exited');
    cluster.fork();
  });
} else {
  http.createServer((req, res) => res.end('ok')).listen(3000);
}

Rules

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Cluster; nodejs/node (GitHub), doc/api/cluster.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Node.js API docs, Cluster; nodejs/node (GitHub), doc/api/cluster.md. MIT (https://raw.githubusercontent.com/nodejs/node/main/LICENSE). Copyright Node.js contributors.

Cite this

Permanent ID: iil:note:fwdj1x6v

iinventedlight. 2026. "Node.js cluster - fork workers and share a server port." iinventedlight Library, iil:note:fwdj1x6v, version of 2026-11-18. https://iinventedlight.com/library/notes/node-cluster/. License: CC-BY-4.0.

@misc{iil_note_fwdj1x6v,
  author = {{iinventedlight}},
  title = {{Node.js cluster - fork workers and share a server port}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-18},
  url = {https://iinventedlight.com/library/notes/node-cluster/},
  note = {Permanent ID iil:note:fwdj1x6v. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/node-cluster.md.