我正在运行 Express 3.5.3 和 Node 0.10.29。当 JSON 未正确解析时,我希望应用程序崩溃并重新启动(为此使用 monit)。不幸的是,应用程序在 app.get 处理程序内部时不会崩溃。这是我的测试代码:
app.get("/api/pixel", function(req, res) {
res.send(getPixel(), { 'Content-Type': 'image/gif' }, 200);
JSON.parse("{123,adkljfl]")
});
我希望应用程序在尝试解析错误的 JSON 后崩溃,但事实并非如此。我很想知道为什么。
最佳答案
您是否使用任何错误处理中间件?默认的快速脚手架有这些:
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
如果你想让应用程序崩溃,你可以删除它们,你必须使用像 forever 这样的东西或 pm2以确保您的应用在崩溃时重新启动。
关于javascript - 为什么 Node.js + express 应用程序不会因错误而崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27465023/