Skip to content

Instantly share code, notes, and snippets.

@idx3d
Last active December 16, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idx3d/5382286 to your computer and use it in GitHub Desktop.
Save idx3d/5382286 to your computer and use it in GitHub Desktop.
var express = require('express')
, app = module.exports = express();
app.use(app.router);
// the error handler is strategically
// placed *below* the app.router; if it
// were above it would not receive errors
// from app.get() etc
app.use(error);
// error handling middleware have an arity of 4
// instead of the typical (req, res, next),
// otherwise they behave exactly like regular
// middleware, you may have several of them,
// in different orders etc.
function error(err, req, res, next) {
//res.send(500);
console.log('error here');
}
app.get('/', function(req, res){
// Caught and passed down to the errorHandler middleware
var text = 'test';
res.send(text);
throw new Error('something broke!');
});
app.get('/next', function(req, res, next){
// We can also pass exceptions to next()
process.nextTick(function(){
next(new Error('oh no!'));
});
});
app.listen(3000);
console.log('Express started on port 3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment