0.0.3 • Published 10 years ago

errhandler v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
10 years ago

NPM   Build Status

errhandler


Error handler for Node.js application http server

© Pradeep Mishra, Licensed under the MIT-LICENSE

Example usage

As a middleware for express, connect like node framework

var app = express();
var errHandler = require("errhandler");

// optional 
errHandler.error(function(err, req, res){
    // this.error, this.request, this.response is equal to err, req, res in arguments
    this.response.send(200, "oops!" + err.stack);
});

app.use(errHandler.middleWare);

app.get('/', function(req,res){
    process.nextTick(function(){
        throw new Error("my bad");
    });
});

app.listen(3000);

As a handler for core node.js http server

var http = require('http');
var errHandler = require("errhandler");

http.createServer(function(req, res) {
    var handler = errHandler.handler(req, res);
    
    // optional 
    handler.error(function(err, req, res){
        res.statusCode = 500;
        res.setHeader('content-type', 'text/html');
        res.end(err.stack);
    });
    
    handler.run(function(){
        throw new Error('my error');
    });
    
}).listen(3000);
npm install errhandler --save