我正在试验Node.js,看看它在我的公司里是否可行。作为一个初学者,我试图在没有Express的情况下处理Node (或者是其他选择),事实上,一个未处理的异常会导致整个过程关闭,基本上让我脱离网络,这似乎是一个问题。它非常清楚地说明了not to use an uncaught exception handler对于进程吞噬错误的作用。
如果我把它和domains结合起来,Node.js的cluster module似乎可以很好地处理这个问题。事实上,domains recommend using them to just send out a 500和使用集群,以便您可以实际关闭出错的进程。
我运行的是Windows8EnterpriseMSI,我的操作系统/体系结构使用的是installed Node.js。我已经让Node.js工作了,并且一直在使用它。但是,当我从cluster module tutorial运行示例代码,然后尝试访问http://localhost:8000时,我的浏览器无法连接。如果我在else块中放入一个console.log()调用,它也永远不会命中。
下面是我使用的代码:
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Tried this using what the defaults are from http://nodejs.org/api/cluster.html#cluster_cluster_settings
// The values of these are undefined otherwise. Seems to make no difference.
// Doesn't work if I remove this line either.
cluster.settings = {
execArgv: process.execArgv,
exec: process.argv[1],
args: process.argv.slice(2),
silent: false
};
// 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 {
console.log("This never hits.");
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);
}这是我可以在Windows8企业版x64上解决的问题吗?有没有关于它为什么不工作的解释?
发布于 2017-12-15 20:03:59
在Windows8上使用Visual Studio调试nodejs应用程序时,我偶然发现了这个问题。
检查周围是否有可能阻塞侦听端口的陈旧nodejs进程。令人惊讶的是,当端口被另一个进程阻塞时,listen()没有异常。在我的案例中,我手动终止了陈旧的进程,集群按预期工作。
https://stackoverflow.com/questions/22061569
复制相似问题