Skip to main content
edited title
Link
nowk
  • 33.2k
  • 2
  • 37
  • 41

node express.js error handling on http requests

added 124 characters in body
Source Link
clarkk
  • 1
  • 76
  • 228
  • 370

How to handle errors in http requests?

Here the full error is returned to the client.. How to write the error to the log and return Fatal error to the client?

Express v4.4.4

var express = require('express'),
    app = express(),
    domain = require('domain'),
    port = 3000;

app.use(function(err, req, res, next){
    console.error(err.stack);
    res.send('Fatal error!', 500);
});

app.get('/', function(req, res){
    var d = domain.create();
    d.on('error', function(err){
        console.error('Error', err);
        
        res.send('Fatal error!', 500);
    });
    d.add(req);
    d.add(res);
    d.run(function(){
        //  a is undefined
        a.ddd();
        
        res.send('Success!');
    });
})
.listen(port, function(){
    console.log('Express server listening on port '+port);
});

How to handle errors in http requests?

Here the full error is returned to the client.. How to write the error to the log and return Fatal error to the client?

Express v4.4.4

var express = require('express'),
    app = express(),
    domain = require('domain'),
    port = 3000;

app.get('/', function(req, res){
    var d = domain.create();
    d.on('error', function(err){
        console.error('Error', err);
        
        res.send('Fatal error!', 500);
    });
    d.add(req);
    d.add(res);
    d.run(function(){
        //  a is undefined
        a.ddd();
        
        res.send('Success!');
    });
})
.listen(port, function(){
    console.log('Express server listening on port '+port);
});

How to handle errors in http requests?

Here the full error is returned to the client.. How to write the error to the log and return Fatal error to the client?

Express v4.4.4

var express = require('express'),
    app = express(),
    domain = require('domain'),
    port = 3000;

app.use(function(err, req, res, next){
    console.error(err.stack);
    res.send('Fatal error!', 500);
});

app.get('/', function(req, res){
    var d = domain.create();
    d.on('error', function(err){
        console.error('Error', err);
        
        res.send('Fatal error!', 500);
    });
    d.add(req);
    d.add(res);
    d.run(function(){
        //  a is undefined
        a.ddd();
        
        res.send('Success!');
    });
})
.listen(port, function(){
    console.log('Express server listening on port '+port);
});
Source Link
clarkk
  • 1
  • 76
  • 228
  • 370

node error handling on http requests

How to handle errors in http requests?

Here the full error is returned to the client.. How to write the error to the log and return Fatal error to the client?

Express v4.4.4

var express = require('express'),
    app = express(),
    domain = require('domain'),
    port = 3000;

app.get('/', function(req, res){
    var d = domain.create();
    d.on('error', function(err){
        console.error('Error', err);
        
        res.send('Fatal error!', 500);
    });
    d.add(req);
    d.add(res);
    d.run(function(){
        //  a is undefined
        a.ddd();
        
        res.send('Success!');
    });
})
.listen(port, function(){
    console.log('Express server listening on port '+port);
});