Skip to content
Myr

index.js

This file

Path

examples/error/index.js

47 lines · 1.1 KiB · JavaScript

Last changed by

remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08

History of this file

Take it

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

Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da

lines 1–6why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('../../')

remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08

line 7why, one step back
7 , logger = require('morgan')

Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da

lines 8–10why, one step back
8 , app = module.exports = express()
9 , test = app.get('env') == 'test';10

remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08

line 11why, one step back
11if (!test) app.use(logger('dev'));

Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da

lines 12–37why, one step back
12
13// error handling middleware have an arity of 414// instead of the typical (req, res, next),15// otherwise they behave exactly like regular16// middleware, you may have several of them,17// in different orders etc.1819function error(err, req, res, next) {20 // log it21 if (!test) console.error(err.stack);2223 // respond with 500 "Internal Server Error".24 res.send(500);25}2627app.get('/', function(req, res){28 // Caught and passed down to the errorHandler middleware29 throw new Error('something broke!');30});3132app.get('/next', function(req, res, next){33 // We can also pass exceptions to next()34 process.nextTick(function(){35 next(new Error('oh no!'));36 });37});

remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a

lines 38–42why, one step back
38
39// the error handler is placed after routes40// if it were above it would not receive errors41// from app.get() etc42app.use(error);

Merge pull request #1503 from shesek/settings-inheritanceTJ Holowaychukbefore adoption82731da

lines 43–47why, one step back
43
44if (!module.parent) {45 app.listen(3000);46 console.log('Express started on port 3000');47}