简体   繁体   中英

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

I would like to use the pretty-error module in my Express app but am having trouble setting it up properly.

I tried using the shortcut...

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

...but it doesn't work.

I haven't used express before, so I'm not sure if this is the best solution, but this is how I integrated pretty-error with 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


This is the screenshot of the error:

在此处输入图片说明


You can also add this if you don't want to see those lines about express` core files:

pe.skipPackage('express');

and this is how it'll look like:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM