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 Jul 26, 2014
1 parent ad76ebc commit d7500c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 47 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.7.0 / 2014-07-25
==================
Expand Down
77 changes: 30 additions & 47 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
};

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -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');
Expand All @@ -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);
};

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
};
Expand All @@ -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);
Expand Down

0 comments on commit d7500c1

Please sign in to comment.