Skip to content
Myr

index.js

This file

Path

examples/error/index.js

49 lines · 1.1 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than a28b7a8 point at it.

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoptiona28b7a8

lines 1–49why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('../../')7 , app = module.exports = express()8 , test = app.get('env') == 'test';910if (!test) app.use(express.logger('dev'));11app.use(app.router);1213// the error handler is strategically14// placed *below* the app.router; if it15// were above it would not receive errors16// from app.get() etc 17app.use(error);1819// error handling middleware have an arity of 420// instead of the typical (req, res, next),21// otherwise they behave exactly like regular22// middleware, you may have several of them,23// in different orders etc.2425function error(err, req, res, next) {26 // log it27 if (!test) console.error(err.stack);2829 // respond with 500 "Internal Server Error".30 res.send(500);31}3233app.get('/', function(req, res){34 // Caught and passed down to the errorHandler middleware35 throw new Error('something broke!');36});3738app.get('/next', function(req, res, next){39 // We can also pass exceptions to next()40 process.nextTick(function(){41 next(new Error('oh no!'));42 });43});4445/* istanbul ignore next */46if (!module.parent) {47 app.listen(3000);48 console.log('Express started on port 3000');49}