diff --git a/index.js b/index.js index b488e58..a10ea33 100644 --- a/index.js +++ b/index.js @@ -271,7 +271,8 @@ Router.prototype.handle = function handle(req, res, callback) { // store route for dispatch on change if (route) { - req.route = route + req.route = req.route || { __proto__: route } + req.route.baseUrl = req.baseUrl; } // Capture one-time layer values diff --git a/lib/route.js b/lib/route.js index 8b67f2d..713be35 100644 --- a/lib/route.js +++ b/lib/route.js @@ -104,7 +104,8 @@ Route.prototype.dispatch = function dispatch(req, res, done) { method = 'get' } - req.route = this + req.route = req.route || { __proto__: this } + req.route.baseUrl = req.baseUrl; next() diff --git a/test/router.js b/test/router.js index 046bd32..ac2af58 100644 --- a/test/router.js +++ b/test/router.js @@ -411,6 +411,23 @@ describe('Router', function () { .expect('x-is-route', 'true') .expect(200, done) }) + + it('should have the correct baseUrl', function (done) { + var router = new Router() + var inner = new Router() + var server = createServer(router) + + inner[method]('/bar', function handle(req, res) { + res.setHeader('x-route-base', String(req.route.baseUrl === '/foo')) + res.end() + }) + router.use('/foo', inner); + + request(server) + [method]('/foo/bar') + .expect('x-route-base', 'true') + .expect(200, done) + }) }) }) })