Skip to content
Myr

index.js

This file

Path

examples/error-pages/index.js

111 lines · 2.9 KiB · JavaScript

Last changed by

tests: add more acceptance testsDouglas Christopher Wilsonbefore adoptionad9a414

History of this file

Take it

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

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 1–19why, one step back
1/**
2 * Module dependencies.3 */45var express = require('../../')6 , app = module.exports = express()7 , silent = 'test' == process.env.NODE_ENV;89// general config10app.set('views', __dirname + '/views');11app.set('view engine', 'jade');1213// our custom "verbose errors" setting14// which we can use in the templates15// via settings['verbose errors']16app.enable('verbose errors');1718// disable them in production19// use $ NODE_ENV=production node examples/error-pages

tests: add more acceptance testsDouglas Christopher Wilsonbefore adoptionad9a414

line 20why, one step back
20if ('production' == app.settings.env) app.disable('verbose errors');

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 21–106why, one step back
21
22app.use(express.favicon());2324silent || app.use(express.logger('dev'));2526// "app.router" positions our routes 27// above the middleware defined below,28// this means that Express will attempt29// to match & call routes _before_ continuing30// on, at which point we assume it's a 404 because31// no route has handled the request.3233app.use(app.router);3435// Since this is the last non-error-handling36// middleware use()d, we assume 404, as nothing else37// responded.3839// $ curl http://localhost:3000/notfound40// $ curl http://localhost:3000/notfound -H "Accept: application/json"41// $ curl http://localhost:3000/notfound -H "Accept: text/plain"4243app.use(function(req, res, next){44 res.status(404);45 46 // respond with html page47 if (req.accepts('html')) {48 res.render('404', { url: req.url });49 return;50 }5152 // respond with json53 if (req.accepts('json')) {54 res.send({ error: 'Not found' });55 return;56 }5758 // default to plain-text. send()59 res.type('txt').send('Not found');60});6162// error-handling middleware, take the same form63// as regular middleware, however they require an64// arity of 4, aka the signature (err, req, res, next).65// when connect has an error, it will invoke ONLY error-handling66// middleware.6768// If we were to next() here any remaining non-error-handling69// middleware would then be executed, or if we next(err) to70// continue passing the error, only error-handling middleware71// would remain being executed, however here72// we simply respond with an error page.7374app.use(function(err, req, res, next){75 // we may use properties of the error object76 // here and next(err) appropriately, or if77 // we possibly recovered from the error, simply next().78 res.status(err.status || 500);79 res.render('500', { error: err });80});8182// Routes8384app.get('/', function(req, res){85 res.render('index.jade');86});8788app.get('/404', function(req, res, next){89 // trigger a 404 since no other middleware90 // will match /404 after this one, and we're not91 // responding here92 next();93});9495app.get('/403', function(req, res, next){96 // trigger a 403 error97 var err = new Error('not allowed!');98 err.status = 403;99 next(err);100});101102app.get('/500', function(req, res, next){103 // trigger a generic (500) error104 next(new Error('keyboard cat!'));105});106

tests: exclude untestable lines in examples from coverageDouglas Christopher Wilsonbefore adoptionc18c2a8

line 107why, one step back
107/* istanbul ignore next */

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

lines 108–109why, one step back
108if (!module.parent) {
109 app.listen(3000);

tests: exclude untestable lines in examples from coverageDouglas Christopher Wilsonbefore adoptionc18c2a8

line 110why, one step back
110 console.log('Express started on port 3000');

pin jade dev dep so tests do not breakTJ Holowaychukbefore adoptiona10f695

line 111why, one step back
111}