Node.js ';未捕获异常';在http.createServer中

Node.js ';未捕获异常';在http.createServer中,node.js,Node.js,如果出现错误,我想在请求中res.end('server error')。我的代码: http.createServer(function(req,res){ process.on('uncaughtException', function() { res.end('server error') }) handlers(req,res) }).listen(1337) 我的决定出了什么问题?您将收到如下错误/警告: (node) warning: possible Even

如果出现错误,我想在请求中res.end('server error')。我的代码:

http.createServer(function(req,res){
  process.on('uncaughtException', function() {
    res.end('server error')
  })
  handlers(req,res)
}).listen(1337)

我的决定出了什么问题?

您将收到如下错误/警告:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:389:29)
    at Server.<anonymous> (/Users/taf2/work/phonetrac/services/ctm-pool/foo.js:2:11)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1572:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:91:29)
    at Socket.ondata (http.js:1468:22)
    at TCP.onread (net.js:374:27)
尝试一下,我们没有发现任何漏洞,我们的错误得到了处理,连接被关闭,让我们的客户可以自由地继续下一个错误:

ab -n 100 -c 10 http://127.0.0.1:1337/
但是,如果您的服务器执行以下复杂操作,此解决方案将在并发状态下崩溃:

function handleError(err) {
  console.error("Caught exception:", err, err.stack);
  this.end();
  this.emit("error:cleanup");
}

require('http').createServer(function(req, res) {
  var errorHandler = handleError.bind(res)
  process.on("uncaughtException", errorHandler);
  res.on("error:cleanup", function() { process.removeListener("uncaughtException", errorHandler); });

  setTimeout(function() {
    this.is.an.error;
    res.end();
    res.emit("cleanup");
  },1000);

}).listen(1337);
这里的问题是uncaughtException将为所有错误触发,并且它们将重叠。这意味着要捕获这样的全局错误,最好只有一个process.on(“uncaughtException”)处理程序

相反,在上述情况下,您需要:

  • 拥有一个独立的try{}catch{}块-这可能很难正确实现
  • 允许异常取消捕获并使进程崩溃,使用monit或类似的方法重新启动进程
  • 您可以尝试使用带有setInternval清理函数的数组来过滤关闭的连接
下面使用的是setInterval

var connectionIndex = 0;
var connections = [];

function handleError(err) {
  console.error("Caught exception:", err, err.stack);
  connections.forEach(function(conn) {
    conn.end();
  });
}

var server = require('http').createServer(function(req, res) {
  connections.push(res.connection);

  setTimeout(function() {
    this.is.an.error;
    res.end();
  },100);

});

setInterval(function() {
  console.log("conn check: %d", connections.length);
  connections = connections.filter(function(conn) {
    return !conn.destroyed;
  });
},1000);

process.on("uncaughtException", handleError);

server.listen(1337);

我有点喜欢上一个解决方案,但我相信它可能有一些优势,所以使用第二个解决方案,使用监控服务重新启动服务器可能是最好的。

Hmm,是的,出了什么问题?你会很快用完侦听器…ab-n 11-c我能找出与异常相关的连接吗n?例如,假设正在处理30个连接,其中2个在一秒钟内引发异常,我如何在handleException函数中理解异常与哪一个相关?
process.on("uncaughtException", handleError);
var connectionIndex = 0;
var connections = [];

function handleError(err) {
  console.error("Caught exception:", err, err.stack);
  connections.forEach(function(conn) {
    conn.end();
  });
}

var server = require('http').createServer(function(req, res) {
  connections.push(res.connection);

  setTimeout(function() {
    this.is.an.error;
    res.end();
  },100);

});

setInterval(function() {
  console.log("conn check: %d", connections.length);
  connections = connections.filter(function(conn) {
    return !conn.destroyed;
  });
},1000);

process.on("uncaughtException", handleError);

server.listen(1337);
process.on("uncaughtException", handleError);