Skip to content

Commit d7500c1

Browse files
committed
Add app.router reference back
1 parent ad76ebc commit d7500c1

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

Diff for: History.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* change:
1010
- `req.host` now returns host (`hostname:port`) - use `req.hostname` for only hostname
1111
- `req.query` is now a getter instead of a plain property
12+
* add:
13+
- `app.router` is a reference to the base router
1214

1315
4.7.0 / 2014-07-25
1416
==================

Diff for: lib/application.js

+30-47
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,28 @@ var app = exports = module.exports = {};
3232
*/
3333

3434
app.init = function(){
35+
var router = null;
36+
3537
this.cache = {};
3638
this.settings = {};
3739
this.engines = {};
3840
this.defaultConfiguration();
41+
42+
// Setup getting to lazily add base router
43+
Object.defineProperty(this, 'router', {
44+
configurable: true,
45+
enumerable: true,
46+
get: function getrouter() {
47+
if (router === null) {
48+
router = new Router({
49+
caseSensitive: this.enabled('case sensitive routing'),
50+
strict: this.enabled('strict routing')
51+
});
52+
}
53+
54+
return router;
55+
}
56+
});
3957
};
4058

4159
/**
@@ -83,23 +101,6 @@ app.defaultConfiguration = function(){
83101
}
84102
};
85103

86-
/**
87-
* lazily adds the base router if it has not yet been added.
88-
*
89-
* We cannot add the base router in the defaultConfiguration because
90-
* it reads app settings which might be set after that has run.
91-
*
92-
* @api private
93-
*/
94-
app.lazyrouter = function() {
95-
if (!this._router) {
96-
this._router = new Router({
97-
caseSensitive: this.enabled('case sensitive routing'),
98-
strict: this.enabled('strict routing')
99-
});
100-
}
101-
};
102-
103104
/**
104105
* Dispatch a req, res pair into the application. Starts pipeline processing.
105106
*
@@ -110,21 +111,12 @@ app.lazyrouter = function() {
110111
*/
111112

112113
app.handle = function(req, res, done) {
113-
var router = this._router;
114-
115114
// final handler
116115
done = done || finalhandler(req, res, {
117116
env: this.get('env'),
118117
onerror: logerror.bind(this)
119118
});
120119

121-
// no routes
122-
if (!router) {
123-
debug('no routes defined on app');
124-
done();
125-
return;
126-
}
127-
128120
// set powered by header
129121
if (this.enabled('x-powered-by')) {
130122
res.setHeader('X-Powered-By', 'Express');
@@ -143,7 +135,7 @@ app.handle = function(req, res, done) {
143135
res.locals = Object.create(null);
144136
}
145137

146-
router.handle(req, res, done);
138+
this.router.handle(req, res, done);
147139
};
148140

149141
/**
@@ -170,9 +162,8 @@ app.use = function use(path, fn) {
170162
: fn;
171163
}
172164

173-
// setup router
174-
this.lazyrouter();
175-
var router = this._router;
165+
// get router
166+
var router = this.router;
176167

177168
// express app
178169
if (mount_app && mount_app.handle && mount_app.set) {
@@ -212,9 +203,8 @@ app.use = function use(path, fn) {
212203
* @api public
213204
*/
214205

215-
app.route = function(path){
216-
this.lazyrouter();
217-
return this._router.route(path);
206+
app.route = function route(path) {
207+
return this.router.route(path);
218208
};
219209

220210
/**
@@ -270,18 +260,15 @@ app.engine = function(ext, fn){
270260
* @api public
271261
*/
272262

273-
app.param = function(name, fn){
274-
var self = this;
275-
self.lazyrouter();
276-
263+
app.param = function param(name, fn) {
277264
if (Array.isArray(name)) {
278-
name.forEach(function(key) {
279-
self.param(key, fn);
280-
});
265+
for (var i = 0; i < name.length; i++) {
266+
this.param(name[i], fn);
267+
}
281268
return this;
282269
}
283270

284-
self._router.param(name, fn);
271+
this.router.param(name, fn);
285272
return this;
286273
};
287274

@@ -418,9 +405,7 @@ methods.forEach(function(method){
418405
app[method] = function(path){
419406
if ('get' == method && 1 == arguments.length) return this.set(path);
420407

421-
this.lazyrouter();
422-
423-
var route = this._router.route(path);
408+
var route = this.route(path);
424409
route[method].apply(route, [].slice.call(arguments, 1));
425410
return this;
426411
};
@@ -437,9 +422,7 @@ methods.forEach(function(method){
437422
*/
438423

439424
app.all = function(path){
440-
this.lazyrouter();
441-
442-
var route = this._router.route(path);
425+
var route = this.route(path);
443426
var args = [].slice.call(arguments, 1);
444427
methods.forEach(function(method){
445428
route[method].apply(route, args);

0 commit comments

Comments
 (0)