Last active
August 29, 2015 14:17
-
-
Save adon-at-work/81842b80c9612feeb835 to your computer and use it in GitHub Desktop.
express-error-handling-examples.js
This file contains hidden or 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'); | |
var app = express(); | |
app.get('/', function (req, res) { | |
res.send('hello world! ' | |
+ '<br/><a href="error">An example throwing error</a>' | |
+ '<br/><a href="error-handled">An example throwing error, but handled').end(); | |
}); | |
app.get('/error', function (req, res) { | |
JSON.parse('invalidJSON'); | |
}); | |
app.get('/error-handled', function (req, res) { | |
JSON.parse('invalidJSON'); | |
}); | |
// Error handler registered at last to catch all errors thrown | |
// Reference: http://expressjs.com/guide/error-handling.html | |
app.use('/error-handled', function (err, req, res, next) { | |
console.error(err); | |
res.status(500).send('Error occurred. Contact admin!'); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment