Last active
October 27, 2025 05:29
-
-
Save rupinder-developer/3040659266a6c3f3c0c55191b712e40d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Here's all you have to do to add clustering to your node.js application. | |
| - save this code as `cluster.js`, and run `cluster.js` instead of `server.js` | |
| - the only line you'll need to change is the last line - it needs to point to the location of your `server.js` file | |
| */ | |
| var cluster = require('cluster'); | |
| if (cluster.isMaster) { | |
| // Count the machine's CPUs | |
| var cpuCount = require('os').cpus().length; | |
| // Create a worker for each CPU | |
| for (var i = 0; i < cpuCount; i += 1) { | |
| cluster.fork(); | |
| } | |
| // Listen for dying workers | |
| cluster.on('exit', function () { | |
| cluster.fork(); | |
| }); | |
| } else { | |
| require('./server'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment