好的,所以我有一个问题。如果在处理 HTTP 请求时发生未捕获的异常,我将没有机会调用 http.ServerResponse 对象的 end() 方法。因此,服务器永远挂起并且永远不会满足请求。
这是一个例子:
var express = require('express');
var app = express.createServer();
var reqNum = 0;
app.get('/favicon.ico', function(req, res) {res.send(404);});
app.get('*', function(req, res, next) {
console.log("Request #", ++reqNum, ":", req.url);
next();
});
app.get('/error', function(req, res, next) {
throw new Error("Problem occurred");
});
app.get('/hang', function(req, res, next) {
console.log("In /hang route");
setTimeout(function() {
console.log("In /hang callback");
if(reqNum >= 3)
throw new Error("Problem occurred");
res.send("It worked!");
}, 2000);
});
process.on('uncaughtException', function(err) {
console.log("Uncaught exception!", err);
});
app.listen(8080);
如果访问/error,会发生异常,但会被捕获。用户收到一条错误消息 - 没问题。但是,如果我访问 /hang,服务器最终将抛出未捕获的异常并永远挂起。对 /hang 的任何后续请求都将挂起。
这很糟糕。有关如何解决此问题的任何建议?