Skip to content
Myr

app.js

This file

Path

examples/error/app.js

45 lines · 1015 bytes · JavaScript

Last changed by

fixed error exampleTJ Holowaychukbefore adoption68bc592

History of this file

Take it

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

new ViewTj Holowaychukbefore adoptione1da383

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

fixed error exampleTJ Holowaychukbefore adoption68bc592

lines 6–7why, one step back
6var express = require('../../')
7 , app = express();

new ViewTj Holowaychukbefore adoptione1da383

line 8why, one step back
8

fixed error exampleTJ Holowaychukbefore adoption68bc592

lines 9–10why, one step back
9app.use(express.logger('dev'));
10app.use(app.router);

new ViewTj Holowaychukbefore adoptione1da383

line 11why, one step back
11

fixed error exampleTJ Holowaychukbefore adoption68bc592

lines 12–31why, one step back
12// the error handler is strategically
13// placed *below* the app.router; if it14// were above it would not receive errors15// from app.get() etc 16app.use(error);1718// error handling middleware have an arity of 419// instead of the typical (req, res, next),20// otherwise they behave exactly like regular21// middleware, you may have several of them,22// in different orders etc.2324function error(err, req, res, next) {25 // log it26 console.error(err.stack);2728 // respond with 500 "Internal Server Error".29 res.send(500);30}31

new ViewTj Holowaychukbefore adoptione1da383

lines 32–38why, one step back
32app.get('/', function(req, res){
33 // Caught and passed down to the errorHandler middleware34 throw new Error('something broke!');35});3637app.get('/next', function(req, res, next){38 // We can also pass exceptions to next()

fixed error exampleTJ Holowaychukbefore adoption68bc592

lines 39–41why, one step back
39 process.nextTick(function(){
40 next(new Error('oh no!'));41 });

new ViewTj Holowaychukbefore adoptione1da383

lines 42–45why, one step back
42});
4344app.listen(3000);45console.log('app listening on port 3000');