node.js - Express error handling or domain? -



node.js - Express error handling or domain? -

i building app in nodejs , express. can help me understand difference between next pieces of code:

var app = express(); app.use(function(err, req, res, next){ console.error(err); res.render('home.ejs', {message:'something broke!'}); });

and

var domain = require('domain'); var d = domain.create(); d.on('error', function(err) { console.error(err); res.render('home.ejs', {message:'something broke!'}); });

are 2 pieces of code alternative? if yes, 1 more reliable in order avoid app crashing on production?

express wraps each function (req, res, next) {} in try/catch. catches many errors, not async errors. domains grab async errors. catching error , handling 2 different things.

if next error (aka d.on('error', next);) end in going express error middleware, in first example.

so instead of thinking express vs domains, think try/catch vs domains. express should used handle error in either case.

node.js express error-handling uncaught-exception

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -