node.js - 如何使用 Node.js Express 应用程序设置 Pretty-Error 模块?

标签 node.js express

我想使用 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


这是错误的屏幕截图:

enter image description here


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

pe.skipPackage('express');

这就是它的样子:

enter image description here

关于node.js - 如何使用 Node.js Express 应用程序设置 Pretty-Error 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22870165/

相关文章:

javascript - TypeError : req. checkBody 不是函数

mysql - 序列化具有相同外键的多条记录的连接表

node.js - Expressjs 应用程序,使用 websockets 进行聊天。为 websocket 服务器使用不同的端口?

javascript - 从客户端 (AngularJS) 向 Node.js 服务器发送图像

node.js - 如何提取单个数组中的所有金额?

javascript - 在 JavaScript 上循环 2 个数字范围

node.js - 表达4参数条件

node.js - 如何用 super 测试检查不存在的头字段?

javascript - WebWorker 线程高负载并发测试期间出现段错误

node.js - 如何将 API Gateway 授权方上下文传递给 HTTP 集成