# #work. js
Let HTTP = require (' http ');
Let port = Math.Round ((1+math.random ()) *1000);
Http.createserver (function (req, res) {
Console.log (PROCESS.PID);
Res.writehead (200,{' content-type ': ' Text/plain ');
Res.end (' Hello World ');
}). Listen (Port,function () {
Console.log ('----Server start port: ' +port ');
});
# # Master.js
Let fork = require (' child_process '). Fork;
Let CPUs = require (' OS '). CPUs;
Console.log (cpus.length);
Replicate the number of node processes based on the number of CPUs on the current machine
for (let i=0;i<2;i++) {
Fork ('./work.js ');
}
1, start the work.js first, and then start the master.js. You can see the boot work.js when listening to a port, and then start the master.js when the thread listens to the other two ports.
You can start a certain thread service by detecting the number of CPUs in the current system.
NODJS Multi-thread master-slave mode