我有一个标准的套接字服务器(无 HTTP)设置,如下(人为的):
var server = net.createServer(function(c) { //'connection' listener
c.on('data', function(data) {
//do stuff here
//some stuff can result in an exception that isn't caught anywhere downstream,
//so it bubbles up. I try to catch it here.
//this is the same problem as just trying to catch this:
throw new Error("catch me if you can");
});
}).listen(8124, function() { //'listening' listener
console.log('socket server started on port 8124,');
});
现在的问题是我有一些代码抛出了根本没有被捕获的错误,导致服务器崩溃。作为最后的措施,我想在这个水平上捕获他们,但我尝试过的任何方法都失败了。
server.on("错误",....)
c.on("错误",...)
也许我需要访问套接字而不是c
(连接),尽管我不确定如何实现。
我使用的是 Node 0.6.9
谢谢。
最佳答案
process.on('uncaughtException',function(err){
console.log('something terrible happened..')
})
关于sockets - Nodejs : How to catch exception in net. createServer.on ("data",...)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608283/