var cluster = require('cluster');
// 判断是否是主进程
if (cluster.isMaster) {
var numCPUs = require('os').cpus().length;
// 开辟一个cpu执行子进程
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
// 进程失效时自动重启子进程
cluster.on('exit', function() {
console.log('A worker process died, restarting...');
cluster.fork();
});
} else {
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.get('/explode', function(req, res) {
setTimeout(function() {
res.send(this.wont.go.over.well);
}, 1);
});
var server = app.listen(3000, function() {
console.log('Server started on port 3000');
});
}