我正在研究sailsJS。 由于节点应用程序在单线程上运行,我需要使用nodeJS的集群创建实例集群。 但我不想在帆中做什么来实现集群的事情。 在表达它是:
var cluster = require('cluster')
,app = require('express')()
,numWorkers = require('os').cpus().length;
if(cluster.isMaster) {
console.log('Master cluster setting up ' + numWorkers + ' workers...');
for(var i = 0; i < numWorkers; i++)
cluster.fork();
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});
cluster.on('exit', function(worker, code, signal) {
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal);
console.log('Starting a new worker');
cluster.fork();
});
} else {
app.all('/*', function(req, res) {res.send('process ' + process.pid + ' says hello!').end();})
var server = app.listen(8000, function() {
console.log('Process ' + process.pid + ' is listening to all incoming requests');
});
}