我想使用 pretty-error我的 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');
这就是它的样子:
关于node.js - 如何使用 Node.js Express 应用程序设置 Pretty-Error 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870165/