我是在学cluster
的时候,发现这个问题的,所以代码就是那一段cluster
的代码。其中有三句注释了,就是得到data的三句,注释 后,连’end’都监听不到了,这是什么情况?
ps:不相关代码有点多,汗。。。
var http = require('http');
var os = require('os');
var cluster = require('cluster');
if(cluster.isMaster){
console.log('init master...');
for(var i = 0; i < os.cpus().length; i++){
cluster.fork();
}
for(var item in cluster.workers){
cluster.workers[item].on('message', function () {
for(var id in cluster.workers){
cluster.workers[id].send('console');
}
});
}
}else{
console.log('init worker' + process.pid);
var count = 0;
http.createServer(function (req, res) {
count += 1;
console.log('worker ' + process.pid + ' been visited\n');
console.log('worker ' + process.pid + ' count = ' + count + '\n');
var data = '';
//req.on('data', function (part) {
// data += part;
//});
req.on('end', function () {
console.log("'end' is triggered");//不会进入了
process.send('visited');
res.end();
});
}).listen(3000);
cluster.worker.on('message', function (m) {
if(m == 'console'){
console.log('worker ' + process.pid + ' count = ' + count + '\n');
}
});
}