Last active
December 16, 2015 05:09
-
-
Save idx3d/5382286 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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