Add app.router reference back
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 78e5054 · Browse files · 2 files·+32 added−46 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−46 removed
- other:+32 added−46 removed
Provenance
- Commit
509ebb1- Tree
286b01e- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
78e5054
2 files changed+32 added−46 removed
modified:lib/application.js+30 added-46 removed
32 unmodified lines33 * @api private34 */35 36 app.init = function(){37+ var router = null;38+39 this.cache = {};40 this.settings = {};41 this.engines = {};42 this.defaultConfiguration();43+44+ // Setup getting to lazily add base router45+ Object.defineProperty(this, 'router', {46+ configurable: true,47+ enumerable: true,48+ get: function getrouter() {49+ if (router === null) {50+ router = new Router({51+ caseSensitive: this.enabled('case sensitive routing'),52+ strict: this.enabled('strict routing')53+ });54+ }55+56+ return router;57+ }58+ });59 };60 61 /**62 * Initialize application configuration.40 unmodified lines103 }104 };105 106 /**89- * lazily adds the base router if it has not yet been added.90- *91- * We cannot add the base router in the defaultConfiguration because92- * it reads app settings which might be set after that has run.93- *94- * @api private95- */96-app.lazyrouter = function() {97- if (!this._router) {98- this._router = new Router({99- caseSensitive: this.enabled('case sensitive routing'),100- strict: this.enabled('strict routing')101- });102- }103-};104-105-/**107 * Dispatch a req, res pair into the application. Starts pipeline processing.108 *109 * If no _done_ callback is provided, then default error handlers will respond110 * in the event of an error bubbling through the stack.1 unmodified line112 * @api private113 */114 115 app.handle = function(req, res, done) {115- var router = this._router;116-116 // final handler117 done = done || finalhandler(req, res, {118 env: this.get('env'),119 onerror: logerror.bind(this)120 });121 123- // no routes124- if (!router) {125- debug('no routes defined on app');126- done();127- return;128- }129-122 // set powered by header123 if (this.enabled('x-powered-by')) {124 res.setHeader('X-Powered-By', 'Express');125 }10 unmodified lines136 if (!res.locals) {137 res.locals = Object.create(null);138 }139 148- router.handle(req, res, done);140+ this.router.handle(req, res, done);141 };142 143 /**144 * Proxy `Router#use()` to add middleware to the app router.30 unmodified lines175 if (fns.length === 0) {176 throw new TypeError('app.use() requires middleware functions');177 }178 187- // setup router188- this.lazyrouter();189- var router = this._router;179+ // get router180+ var router = this.router;181 182 fns.forEach(function (fn) {183 // non-express app184 if (!fn || !fn.handle || !fn.set) {30 unmodified lines215 *216 * @api public217 */218 228-app.route = function(path){229- this.lazyrouter();230- return this._router.route(path);219+app.route = function route(path) {220+ return this.router.route(path);221 };222 223 /**224 * Register the given template engine callback `fn`47 unmodified lines272 * @return {app} for chaining273 * @api public274 */275 286-app.param = function(name, fn){287- this.lazyrouter();288-276+app.param = function param(name, fn) {277 if (Array.isArray(name)) {290- name.forEach(function(key) {291- this.param(key, fn);292- }, this);278+ for (var i = 0; i < name.length; i++) {279+ this.param(name[i], fn);280+ }281 return this;282 }283 296- this._router.param(name, fn);284+ this.router.param(name, fn);285 return this;286 };287 288 /**128 unmodified lines417 methods.forEach(function(method){418 app[method] = function(path){419 if ('get' == method && 1 == arguments.length) return this.set(path);420 433- this.lazyrouter();434-435- var route = this._router.route(path);421+ var route = this.route(path);422 route[method].apply(route, slice.call(arguments, 1));423 return this;424 };425 });8 unmodified lines434 * @api public435 */436 437 app.all = function(path){452- this.lazyrouter();453-454- var route = this._router.route(path);438+ var route = this.route(path);439 var args = slice.call(arguments, 1);440 methods.forEach(function(method){441 route[method].apply(route, args);442 });120 unmodified linesmodified:History.md+2 added
7 unmodified lines8 - `express.query` middleware9 * change:10 - `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname11 - `req.query` is now a getter instead of a plain property12+ * add:13+ - `app.router` is a reference to the base router14 15 4.10.1 / 2014-10-2816 ===================17 2447 unmodified lines