Renamed pages example to error-pages. Closes #459
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 c4ba1b0 · Browse files · 10 files·+123 added−123 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+123 added−123 removed
- other:+123 added−123 removed
Provenance
- Commit
0526a24- Tree
be8c584- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
c4ba1b0
10 files changed+123 added−123 removed
added:examples/error-pages/views/404.jade+4 added
1+- if (error.path)2+ h2 Cannot find #{error.path}3+- else4+ h2 Page Not Foundadded:examples/error-pages/views/500.jade+2 added
1+h1 Error: #{error.message}2+pre= error.stackadded:examples/error-pages/views/index.jade+8 added
1+h2 Pages Example2+ul3+ li 4+ | visit 5+ a(href="/500") 5006+ li7+ | visit8+ a(href="/404") 404added:examples/error-pages/views/layout.jade+6 added
1+html2+ head 3+ title Custom Pages Example4+ body5+ h1 My Site6+ != bodyadded:examples/error-pages/app.js+103 added
1+2+// Expose modules in ./support for demo purposes3+require.paths.unshift(__dirname + '/../../support');4+5+/**6+ * Module dependencies.7+ */8+9+var express = require('./../../lib/express');10+11+var app = express.createServer(),12+ sys = require('sys');13+14+// Serve default connect favicon15+app.use(express.favicon());16+17+// Logger is placed below favicon, so favicon.ico18+// requests will not be logged19+app.use(express.logger({ format: '":method :url" :status' }));20+21+// "app.router" positions our routes 22+// specifically above the middleware23+// assigned below24+25+app.use(app.router);26+27+// When no more middleware require execution, aka28+// our router is finished and did not respond, we29+// can assume that it is "not found". Instead of30+// letting Connect deal with this, we define our31+// custom middleware here to simply pass a NotFound32+// exception33+34+app.use(function(req, res, next){35+ next(new NotFound(req.url));36+});37+38+app.set('views', __dirname + '/views');39+40+// Provide our app with the notion of NotFound exceptions41+42+function NotFound(path){43+ this.name = 'NotFound';44+ if (path) {45+ Error.call(this, 'Cannot find ' + path);46+ this.path = path;47+ } else {48+ Error.call(this, 'Not Found');49+ }50+ Error.captureStackTrace(this, arguments.callee);51+}52+53+sys.inherits(NotFound, Error);54+55+// We can call app.error() several times as shown below.56+// Here we check for an instanceof NotFound and show the57+// 404 page, or we pass on to the next error handler.58+59+// These handlers could potentially be defined within60+// configure() blocks to provide introspection when61+// in the development environment.62+63+app.error(function(err, req, res, next){64+ if (err instanceof NotFound) {65+ res.render('404.jade', {66+ status: 404,67+ locals: {68+ error: err69+ }70+ });71+ } else {72+ next(err);73+ }74+});75+76+// Here we assume all errors as 500 for the simplicity of77+// this demo, however you can choose whatever you like78+79+app.error(function(err, req, res){80+ res.render('500.jade', {81+ status: 500,82+ locals: {83+ error: err84+ } 85+ });86+});87+88+// Routes89+90+app.get('/', function(req, res){91+ res.render('index.jade');92+});93+94+app.get('/404', function(req, res){95+ throw new NotFound;96+});97+98+app.get('/500', function(req, res, next){99+ next(new Error('keyboard cat!'));100+});101+102+app.listen(3000);103+console.log('Express app started on port 3000');deleted:examples/pages/views/404.jade-4 removed
1-- if (error.path)2- h2 Cannot find #{error.path}3-- else4- h2 Page Not Founddeleted:examples/pages/views/500.jade-2 removed
1-h1 Error: #{error.message}2-pre= error.stackdeleted:examples/pages/views/index.jade-8 removed
1-h2 Pages Example2-ul3- li 4- | visit 5- a(href="/500") 5006- li7- | visit8- a(href="/404") 404deleted:examples/pages/views/layout.jade-6 removed
1-html2- head 3- title Custom Pages Example4- body5- h1 My Site6- != bodydeleted:examples/pages/app.js-103 removed
1-2-// Expose modules in ./support for demo purposes3-require.paths.unshift(__dirname + '/../../support');4-5-/**6- * Module dependencies.7- */8-9-var express = require('./../../lib/express');10-11-var app = express.createServer(),12- sys = require('sys');13-14-// Serve default connect favicon15-app.use(express.favicon());16-17-// Logger is placed below favicon, so favicon.ico18-// requests will not be logged19-app.use(express.logger({ format: '":method :url" :status' }));20-21-// "app.router" positions our routes 22-// specifically above the middleware23-// assigned below24-25-app.use(app.router);26-27-// When no more middleware require execution, aka28-// our router is finished and did not respond, we29-// can assume that it is "not found". Instead of30-// letting Connect deal with this, we define our31-// custom middleware here to simply pass a NotFound32-// exception33-34-app.use(function(req, res, next){35- next(new NotFound(req.url));36-});37-38-app.set('views', __dirname + '/views');39-40-// Provide our app with the notion of NotFound exceptions41-42-function NotFound(path){43- this.name = 'NotFound';44- if (path) {45- Error.call(this, 'Cannot find ' + path);46- this.path = path;47- } else {48- Error.call(this, 'Not Found');49- }50- Error.captureStackTrace(this, arguments.callee);51-}52-53-sys.inherits(NotFound, Error);54-55-// We can call app.error() several times as shown below.56-// Here we check for an instanceof NotFound and show the57-// 404 page, or we pass on to the next error handler.58-59-// These handlers could potentially be defined within60-// configure() blocks to provide introspection when61-// in the development environment.62-63-app.error(function(err, req, res, next){64- if (err instanceof NotFound) {65- res.render('404.jade', {66- status: 404,67- locals: {68- error: err69- }70- });71- } else {72- next(err);73- }74-});75-76-// Here we assume all errors as 500 for the simplicity of77-// this demo, however you can choose whatever you like78-79-app.error(function(err, req, res){80- res.render('500.jade', {81- status: 500,82- locals: {83- error: err84- } 85- });86-});87-88-// Routes89-90-app.get('/', function(req, res){91- res.render('index.jade');92-});93-94-app.get('/404', function(req, res){95- throw new NotFound;96-});97-98-app.get('/500', function(req, res, next){99- next(new Error('keyboard cat!'));100-});101-102-app.listen(3000);103-console.log('Express app started on port 3000');