fixed error-pages example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 68bc592 · Browse files · 3 files·+45 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+45 added−33 removed
- other:+45 added−33 removed
Provenance
- Commit
4fa9a2e- Tree
aa5d629- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
68bc592
3 files changed+45 added−33 removed
modified:examples/error-pages/views/index.jade+15 added-11 removed
1+2+extends layout3+4+block content1-h2 Pages Example2-ul3- li 4- | visit 5- a(href="/500") 5006- li7- | visit8- a(href="/404") 4049- li10- | visit11- a(href='/403') 4035+ h2 Pages Example6+ ul7+ li 8+ | visit 9+ a(href="/500") 50010+ li11+ | visit12+ a(href="/404") 40413+ li14+ | visit15+ a(href='/403') 403modified:examples/error-pages/views/layout.jade+1 added-1 removed
1 unmodified line2 head 3 title Custom Pages Example4 body5 h1 My Site6- != body6+ block contentmodified:examples/error-pages/app.js+29 added-21 removed
12342345967811910111512131418192021152324161718192021222324252627282930313233343536333435363738394041424344454647484950516 unmodified lines5859606151626364655657585966676869707120 unmodified lines
// Expose modules in ./support for demo purposesrequire.paths.unshift(__dirname + '/../../support');
/** * Module dependencies. */
var express = require('../../lib/express');var express = require('../../') , app = express();
var app = express.createServer();app.set('views', __dirname + '/views');app.set('view engine', 'jade');
// Serve default connect faviconapp.use(express.favicon());app.use(express.logger('dev'));
// Logger is placed below favicon, so favicon.ico// requests will not be loggedapp.use(express.logger('":method :url" :status'));
// "app.router" positions our routes // specifically above the middleware// assigned below// above the middleware defined below,// this means that Express will attempt// to match & call routes _before_ continuing// on, at which point we assume it's a 404 because// no route has handled the request.
app.use(app.router);
// Since this is the last non-error-handling// middleware use()d, we assume 404, as nothing else// responded.
// $ curl http://localhost:3000/notfound// $ curl http://localhost:3000/notfound -H "Accept: application/json"// $ curl http://localhost:3000/notfound -H "Accept: text/plain"
app.use(function(req, res, next){ // the status option, or res.statusCode = 404 // are equivalent, however with the option we // get the "status" local available as well res.render('404', { status: 404, url: req.url }); // respond with html page if (req.accepts('html')) { res.status(404); res.render('404', { url: req.url }); return; }
// respond with json if (req.accepts('json')) { res.send({ error: 'Not found' }); return; }
// default to plain-text. send() res.type('txt').send('Not found');});
// error-handling middleware, take the same form// as regular middleware, however they require an6 unmodified lines// continue passing the error, only error-handling middleware// would remain being executed, however here// we simply respond with an error page.
app.use(function(err, req, res, next){ // we may use properties of the error object // here and next(err) appropriately, or if // we possibly recovered from the error, simply next(). res.render('500', { status: err.status || 500 , error: err }); res.status(err.status || 500); res.render('500', { error: err });});
// Routes
20 unmodified lines