mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 12:19:51 +01:00
app.path()
This commit is contained in:
parent
2a13db3bb8
commit
17a831e32f
|
|
@ -171,6 +171,7 @@ app.match = function(url){
|
|||
app.use = function(route, fn){
|
||||
var app, home, handle;
|
||||
|
||||
// default route to '/'
|
||||
if ('string' != typeof route) fn = route, route = '/';
|
||||
|
||||
// express app
|
||||
|
|
@ -344,6 +345,21 @@ app.set = function(setting, val){
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Return the app's absolute pathname
|
||||
* based on the parent(s) that have
|
||||
* mounted it.
|
||||
*
|
||||
* @return {String}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
app.path = function(){
|
||||
return this.parent
|
||||
? this.parent.path() + this.route
|
||||
: '';
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if `setting` is enabled.
|
||||
*
|
||||
|
|
|
|||
48
test/app.js
48
test/app.js
|
|
@ -1,5 +1,6 @@
|
|||
|
||||
var express = require('../');
|
||||
var express = require('../')
|
||||
, assert = require('assert');
|
||||
|
||||
describe('app', function(){
|
||||
it('should inherit from event emitter', function(done){
|
||||
|
|
@ -8,3 +9,48 @@ describe('app', function(){
|
|||
app.emit('foo');
|
||||
})
|
||||
})
|
||||
|
||||
describe('app.parent', function(){
|
||||
it('should return the parent when mounted', function(){
|
||||
var app = express()
|
||||
, blog = express()
|
||||
, blogAdmin = express();
|
||||
|
||||
app.use('/blog', blog);
|
||||
blog.use('/admin', blogAdmin);
|
||||
|
||||
assert(!app.parent, 'app.parent');
|
||||
blog.parent.should.equal(app);
|
||||
blogAdmin.parent.should.equal(blog);
|
||||
})
|
||||
})
|
||||
|
||||
describe('app.route', function(){
|
||||
it('should return the mounted path', function(){
|
||||
var app = express()
|
||||
, blog = express()
|
||||
, blogAdmin = express();
|
||||
|
||||
app.use('/blog', blog);
|
||||
blog.use('/admin', blogAdmin);
|
||||
|
||||
app.route.should.equal('/');
|
||||
blog.route.should.equal('/blog');
|
||||
blogAdmin.route.should.equal('/admin');
|
||||
})
|
||||
})
|
||||
|
||||
describe('app.path()', function(){
|
||||
it('should return the canonical', function(){
|
||||
var app = express()
|
||||
, blog = express()
|
||||
, blogAdmin = express();
|
||||
|
||||
app.use('/blog', blog);
|
||||
blog.use('/admin', blogAdmin);
|
||||
|
||||
app.path().should.equal('');
|
||||
blog.path().should.equal('/blog');
|
||||
blogAdmin.path().should.equal('/blog/admin');
|
||||
})
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user