[英]Why doesn't Node.js + express app crash on Error?
我正在運行Express 3.5.3和節點0.10.29。 我希望當未正確解析JSON時,應用崩潰並重新啟動(為此使用監控)。 不幸的是,在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: {}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.