app.js
This file
Path
examples/error/app.js
49 lines · 1.1 KiB · JavaScript
Last changed by
Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a
History of this fileTake it
The walk stopped after 200 commits: lines older than bf87d62 point at it.
docsTj Holowaychukbefore adoptionbf87d62
lines 1–5why, one step back12/**3 * Module dependencies.4 */5mergedTj Holowaychukbefore adoption547d18c
line 6why, one step back6var express = require('../../')Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a
line 7why, one step back7 , app = module.exports = express();docsTj Holowaychukbefore adoptionbf87d62
line 8why, one step back8Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a
lines 9–10why, one step back9if ('test' != process.env.NODE_ENV)10 app.use(express.logger('dev'));mergedTj Holowaychukbefore adoption547d18c
line 11why, one step back11app.use(app.router);docsTj Holowaychukbefore adoptionbf87d62
line 12why, one step back12mergedTj Holowaychukbefore adoption547d18c
lines 13–26why, one step back13// 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 itMerge branch 'integration'TJ Holowaychukbefore adoption0ae024a
lines 27–28why, one step back27 if ('test' != process.env.NODE_ENV)28 console.error(err.stack);mergedTj Holowaychukbefore adoption547d18c
lines 29–33why, one step back2930 // respond with 500 "Internal Server Error".31 res.send(500);32}33docsTj Holowaychukbefore adoptionbf87d62
lines 34–40why, one step back34app.get('/', function(req, res){35 // Caught and passed down to the errorHandler middleware36 throw new Error('something broke!');37});3839app.get('/next', function(req, res, next){40 // We can also pass exceptions to next()mergedTj Holowaychukbefore adoption547d18c
lines 41–43why, one step back41 process.nextTick(function(){42 next(new Error('oh no!'));43 });docsTj Holowaychukbefore adoptionbf87d62
lines 44–45why, one step back44});45Merge branch 'integration'TJ Holowaychukbefore adoption0ae024a
lines 46–49why, one step back46if (!module.parent) {47 app.listen(3000);48 console.log('Express started on port 3000');49}