[英]Nodejs cluster isn't response to web browser
I am a newbie in Nodejs and I try something with cluster on nodejs. 我是Nodejs的新手,并且尝试在nodejs上使用集群。 But I meet a problem: - I use example on Nodejs API about Cluster:
但是我遇到一个问题:-我在Nodejs API上使用了有关集群的示例:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}
How can I get this server to respond to requests? 如何使该服务器响应请求?
p/s: I try getting event "exit" to respawn new worker. p / s:我尝试获取事件“ exit”以重新生成新的worker。 Sometime when I hit enter on browser, console log worker x die and then respawn new worker.
有时,当我在浏览器中按下Enter键时,控制台日志工作程序x死亡,然后重新生成新的工作程序。 But browser still connecting...
但是浏览器仍在连接...
http://i.stack.imgur.com/RHSYY.png http://i.stack.imgur.com/RHSYY.png
Help me :) and sorry my bad english 帮帮我:)抱歉我的英语不好
I install the newest version of nodejs ( v0.10.24) 我安装了最新版本的nodejs(v0.10.24)
I had a similar issue with cluster. 我在集群中遇到了类似的问题。 Try putting the http server creation part inside a setTimeout call.
尝试将http服务器创建部分放在setTimeout调用中。 (for example delay it for 1000 ms) you should observe an improvement.
(例如,将其延迟1000毫秒),您应该会有所改善。
setTimeout(function(){ http.createServer...... }, 1000);
Besides, you may also try to create your server using jxcore mt-keep to see if it works on the similar case. 此外,您还可以尝试使用jxcore mt-keep创建服务器,以查看它是否适用于类似情况。
$ jx mt-keep servercode.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.