簡體   English   中英

為什么Node.js + Express不會因錯誤而崩潰?

[英]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: {}
    });
});

如果您希望應用程序崩潰,您可以刪除它們,你必須使用像永遠PM2 ,以確保您的應用程序被重啟死機。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM