nodejs中處理回調函數的異常
阿新 • • 發佈:2017-06-01
out dom tac rip ack javascrip java logger exception
假設是使用nodejs+express3這個經典的組合。那麽有一種非常方面的處理回調函數異常的方法:
1. 安裝模塊:express-domain-middleware
2. 增加例如以下的代碼:
app.use(require(‘express-domain-middleware‘)); app.use(function errorHandler(err, req, res, next) { logger.error(‘error on request %d %s %s: %j‘, process.domain.id, req.method, req.url, err); res.send(500, "there is an error in callback function"); if(err.domain) { //you should think about gracefully stopping & respawning your server //since an unhandled error might put your application into an unknown state } }); app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
nodejs中處理回調函數的異常