Skip to content
Myr

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

10 files+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 Found
added:examples/error-pages/views/500.jade+2 added
1h1 Error: #{error.message}2pre= error.stack
added:examples/error-pages/views/index.jade+8 added
1h2 Pages Example2ul3 li 4 | visit 5 a(href="/500") 5006 li7 | visit8 a(href="/404") 404
added:examples/error-pages/views/layout.jade+6 added
1html2 head 3 title Custom Pages Example4 body5 h1 My Site6 != body
added:examples/error-pages/app.js+103 added
12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */89var express = require('./../../lib/express');1011var app = express.createServer(),12 sys = require('sys');1314// Serve default connect favicon15app.use(express.favicon());1617// Logger is placed below favicon, so favicon.ico18// requests will not be logged19app.use(express.logger({ format: '":method :url" :status' }));2021// "app.router" positions our routes 22// specifically above the middleware23// assigned below2425app.use(app.router);2627// 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// exception3334app.use(function(req, res, next){35 next(new NotFound(req.url));36});3738app.set('views', __dirname + '/views');3940// Provide our app with the notion of NotFound exceptions4142function 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}5253sys.inherits(NotFound, Error);5455// 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.5859// These handlers could potentially be defined within60// configure() blocks to provide introspection when61// in the development environment.6263app.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});7576// Here we assume all errors as 500 for the simplicity of77// this demo, however you can choose whatever you like7879app.error(function(err, req, res){80 res.render('500.jade', {81 status: 500,82 locals: {83 error: err84 } 85 });86});8788// Routes8990app.get('/', function(req, res){91 res.render('index.jade');92});9394app.get('/404', function(req, res){95 throw new NotFound;96});9798app.get('/500', function(req, res, next){99 next(new Error('keyboard cat!'));100});101102app.listen(3000);103console.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 Found
deleted:examples/pages/views/500.jade-2 removed
1h1 Error: #{error.message}2pre= error.stack
deleted:examples/pages/views/index.jade-8 removed
1h2 Pages Example2ul3 li 4 | visit 5 a(href="/500") 5006 li7 | visit8 a(href="/404") 404
deleted:examples/pages/views/layout.jade-6 removed
1html2 head 3 title Custom Pages Example4 body5 h1 My Site6 != body
deleted:examples/pages/app.js-103 removed
12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */89var express = require('./../../lib/express');1011var app = express.createServer(),12 sys = require('sys');1314// Serve default connect favicon15app.use(express.favicon());1617// Logger is placed below favicon, so favicon.ico18// requests will not be logged19app.use(express.logger({ format: '":method :url" :status' }));2021// "app.router" positions our routes 22// specifically above the middleware23// assigned below2425app.use(app.router);2627// 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// exception3334app.use(function(req, res, next){35 next(new NotFound(req.url));36});3738app.set('views', __dirname + '/views');3940// Provide our app with the notion of NotFound exceptions4142function 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}5253sys.inherits(NotFound, Error);5455// 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.5859// These handlers could potentially be defined within60// configure() blocks to provide introspection when61// in the development environment.6263app.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});7576// Here we assume all errors as 500 for the simplicity of77// this demo, however you can choose whatever you like7879app.error(function(err, req, res){80 res.render('500.jade', {81 status: 500,82 locals: {83 error: err84 } 85 });86});8788// Routes8990app.get('/', function(req, res){91 res.render('index.jade');92});9394app.get('/404', function(req, res){95 throw new NotFound;96});9798app.get('/500', function(req, res, next){99 next(new Error('keyboard cat!'));100});101102app.listen(3000);103console.log('Express app started on port 3000');