diff --git a/History.md b/History.md index 2ba05b414c..8930fb648f 100644 --- a/History.md +++ b/History.md @@ -9,6 +9,8 @@ * change: - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname - `req.query` is now a getter instead of a plain property + * add: + - `app.router` is a reference to the base router 4.7.0 / 2014-07-25 ================== diff --git a/lib/application.js b/lib/application.js index da7e7c576b..582247c4a4 100644 --- a/lib/application.js +++ b/lib/application.js @@ -32,10 +32,28 @@ var app = exports = module.exports = {}; */ app.init = function(){ + var router = null; + this.cache = {}; this.settings = {}; this.engines = {}; this.defaultConfiguration(); + + // Setup getting to lazily add base router + Object.defineProperty(this, 'router', { + configurable: true, + enumerable: true, + get: function getrouter() { + if (router === null) { + router = new Router({ + caseSensitive: this.enabled('case sensitive routing'), + strict: this.enabled('strict routing') + }); + } + + return router; + } + }); }; /** @@ -83,23 +101,6 @@ app.defaultConfiguration = function(){ } }; -/** - * lazily adds the base router if it has not yet been added. - * - * We cannot add the base router in the defaultConfiguration because - * it reads app settings which might be set after that has run. - * - * @api private - */ -app.lazyrouter = function() { - if (!this._router) { - this._router = new Router({ - caseSensitive: this.enabled('case sensitive routing'), - strict: this.enabled('strict routing') - }); - } -}; - /** * Dispatch a req, res pair into the application. Starts pipeline processing. * @@ -110,21 +111,12 @@ app.lazyrouter = function() { */ app.handle = function(req, res, done) { - var router = this._router; - // final handler done = done || finalhandler(req, res, { env: this.get('env'), onerror: logerror.bind(this) }); - // no routes - if (!router) { - debug('no routes defined on app'); - done(); - return; - } - // set powered by header if (this.enabled('x-powered-by')) { res.setHeader('X-Powered-By', 'Express'); @@ -143,7 +135,7 @@ app.handle = function(req, res, done) { res.locals = Object.create(null); } - router.handle(req, res, done); + this.router.handle(req, res, done); }; /** @@ -170,9 +162,8 @@ app.use = function use(path, fn) { : fn; } - // setup router - this.lazyrouter(); - var router = this._router; + // get router + var router = this.router; // express app if (mount_app && mount_app.handle && mount_app.set) { @@ -212,9 +203,8 @@ app.use = function use(path, fn) { * @api public */ -app.route = function(path){ - this.lazyrouter(); - return this._router.route(path); +app.route = function route(path) { + return this.router.route(path); }; /** @@ -270,18 +260,15 @@ app.engine = function(ext, fn){ * @api public */ -app.param = function(name, fn){ - var self = this; - self.lazyrouter(); - +app.param = function param(name, fn) { if (Array.isArray(name)) { - name.forEach(function(key) { - self.param(key, fn); - }); + for (var i = 0; i < name.length; i++) { + this.param(name[i], fn); + } return this; } - self._router.param(name, fn); + this.router.param(name, fn); return this; }; @@ -418,9 +405,7 @@ methods.forEach(function(method){ app[method] = function(path){ if ('get' == method && 1 == arguments.length) return this.set(path); - this.lazyrouter(); - - var route = this._router.route(path); + var route = this.route(path); route[method].apply(route, [].slice.call(arguments, 1)); return this; }; @@ -437,9 +422,7 @@ methods.forEach(function(method){ */ app.all = function(path){ - this.lazyrouter(); - - var route = this._router.route(path); + var route = this.route(path); var args = [].slice.call(arguments, 1); methods.forEach(function(method){ route[method].apply(route, args);