app.js
This file
Path
examples/error-pages/app.js
96 lines · 2.2 KiB · JavaScript
Last changed by
refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
History of this fileTake it
Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 1–10why, one step back12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */89var express = require('./../../lib/express');10refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 11why, one step back11var app = express.createServer();Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 12–33why, one step back1213// Serve default connect favicon14app.use(express.favicon());1516// Logger is placed below favicon, so favicon.ico17// requests will not be logged18app.use(express.logger({ format: '":method :url" :status' }));1920// "app.router" positions our routes 21// specifically above the middleware22// assigned below2324app.use(app.router);2526// When no more middleware require execution, aka27// our router is finished and did not respond, we28// can assume that it is "not found". Instead of29// letting Connect deal with this, we define our30// custom middleware here to simply pass a NotFound31// exception3233app.use(function(req, res, next){refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 34why, one step back34 next(new NotFound(req.url));Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 35–41why, one step back35});3637app.set('views', __dirname + '/views');3839// Provide our app with the notion of NotFound exceptions4041function NotFound(path){refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
lines 42–49why, one step back42 this.name = 'NotFound';43 if (path) {44 Error.call(this, 'Cannot find ' + path);45 this.path = path;46 } else {47 Error.call(this, 'Not Found');48 }49 Error.captureStackTrace(this, arguments.callee);Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 50–51why, one step back50}51refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
lines 52–54why, one step back52/**53 * Inherit from `Error.prototype`.54 */Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
line 55why, one step back55refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
lines 56–57why, one step back56NotFound.prototype.__proto__ = Error.prototype;57Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 58–67why, one step back58// We can call app.error() several times as shown below.59// Here we check for an instanceof NotFound and show the60// 404 page, or we pass on to the next error handler.6162// These handlers could potentially be defined within63// configure() blocks to provide introspection when64// in the development environment.6566app.error(function(err, req, res, next){67 if (err instanceof NotFound) {refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 68why, one step back68 res.render('404.jade', { status: 404, error: err });Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
line 69why, one step back69 } else {refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 70why, one step back70 next(err);Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 71–77why, one step back71 }72});7374// Here we assume all errors as 500 for the simplicity of75// this demo, however you can choose whatever you like7677app.error(function(err, req, res){refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 78why, one step back78 res.render('500.jade', { status: 500, error: err });Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 79–83why, one step back79});8081// Routes8283app.get('/', function(req, res){refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 84why, one step back84 res.render('index.jade');Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 85–87why, one step back85});8687app.get('/404', function(req, res){refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 88why, one step back88 throw new NotFound(req.url);Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 89–91why, one step back89});9091app.get('/500', function(req, res, next){refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7
line 92why, one step back92 next(new Error('keyboard cat!'));Renamed pages example to error-pages. Closes #459Tj Holowaychukbefore adoption0526a24
lines 93–96why, one step back93});9495app.listen(3000);96console.log('Express app started on port 3000');