簡體   English   中英

如何使用node.js Express應用程序設置漂亮錯誤模塊?

[英]How do you set up pretty-error module with a node.js Express app?

我想在我的Express應用程序中使用漂亮錯誤模塊,但無法正確設置它。

我嘗試使用快捷方式...

require('pretty-error').start(function(){
    http.createServer(app).listen(app.get('port'), function(){
        console.log('Express server listening on port ' + app.get('port'));
    });
});

...但是它不起作用。

我以前沒有使用過express,所以我不確定這是否是最好的解決方案,但這是我將pretty-error與express集成在一起的方式:

// this is app.js

var express = require('express');
var PrettyError = require('pretty-error');

var app = express();

app.get('/', function(req, res) {

   // this will throw an error:
   var a = b;

});

var server = app.listen(3000, function(){

   console.log('Server started \n');

});


// we can now instantiaite Prettyerror
pe = new PrettyError();

// and use it for our app's error handler
app.use(function(err, req, res, next){

   console.log(pe.render(err));

});

// we can optionally configure prettyError to simplify the stack trace:

pe.skipNodeFiles(); // this will skip events.js and http.js and similar core node files


這是錯誤的屏幕截圖:

在此處輸入圖片說明


如果您不想看到有關express`核心文件的那些行,也可以添加此代碼:

pe.skipPackage('express');

它將是這樣的:

在此處輸入圖片說明

暫無
暫無

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

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