Skip to content

Commit

Permalink
Add app.router reference back
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Nov 7, 2014
1 parent 78e5054 commit 509ebb1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
2 changes: 2 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.10.1 / 2014-10-28
===================
Expand Down
76 changes: 30 additions & 46 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,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;
}
});
};

/**
Expand Down Expand Up @@ -85,23 +103,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.
*
Expand All @@ -112,21 +113,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');
Expand All @@ -145,7 +137,7 @@ app.handle = function(req, res, done) {
res.locals = Object.create(null);
}

router.handle(req, res, done);
this.router.handle(req, res, done);
};

/**
Expand Down Expand Up @@ -184,9 +176,8 @@ app.use = function use(fn) {
throw new TypeError('app.use() requires middleware functions');
}

// setup router
this.lazyrouter();
var router = this._router;
// get router
var router = this.router;

fns.forEach(function (fn) {
// non-express app
Expand Down Expand Up @@ -225,9 +216,8 @@ app.use = function use(fn) {
* @api public
*/

app.route = function(path){
this.lazyrouter();
return this._router.route(path);
app.route = function route(path) {
return this.router.route(path);
};

/**
Expand Down Expand Up @@ -283,17 +273,15 @@ app.engine = function(ext, fn){
* @api public
*/

app.param = function(name, fn){
this.lazyrouter();

app.param = function param(name, fn) {
if (Array.isArray(name)) {
name.forEach(function(key) {
this.param(key, fn);
}, this);
for (var i = 0; i < name.length; i++) {
this.param(name[i], fn);
}
return this;
}

this._router.param(name, fn);
this.router.param(name, fn);
return this;
};

Expand Down Expand Up @@ -430,9 +418,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;
};
Expand All @@ -449,9 +435,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);
Expand Down

0 comments on commit 509ebb1

Please sign in to comment.