[英]How to create server instance in master in nodejs cluster? (both in worker and master)
The following code snippet is throwing error. 以下代码段引发错误。 Is there a way to achieve the following functionality ? 有没有办法实现以下功能? If not why it is not possible to create server instance in nodejs cluster master? 如果不是,为什么无法在nodejs群集主服务器中创建服务器实例?
var cluster = require('cluster');
var http = require('http');
if (cluster.isMaster) {
var numCPUs = require('os').cpus().length;
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
// How to create server instance in master?
// It is throwing error when I create server like below.
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello worlddddd\n");
}).listen(8000);
} else {
http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
} }
The error that I got when running this code seems to point to the address:port combination already being in use. 运行此代码时出现的错误似乎指向已经在使用的address:port组合。 Change the port that your master server is listening on, and that should resolve the error. 更改您的主服务器正在侦听的端口,这应该可以解决该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.