From a6ce0a11f35446fd016c0ea09566c0e66b26507d Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Fri, 22 Aug 2014 09:26:14 +0800 Subject: [PATCH] supports custom routing detect path: ctx.routerPath --- lib/router.js | 5 +++-- package.json | 2 +- test/lib/router.js | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/router.js b/lib/router.js index 1d8839c..46f96d3 100644 --- a/lib/router.js +++ b/lib/router.js @@ -61,10 +61,11 @@ router.middleware = function() { this.params = []; } - debug('%s %s', this.method, this.path); + var pathname = this.routerPath || this.path; + debug('%s %s', this.method, pathname); // Find routes matching requested path - if (matchedRoutes = router.match(this.path)) { + if (matchedRoutes = router.match(pathname)) { var methodsAvailable = {}; // Find matched route for requested method diff --git a/package.json b/package.json index 96fff2d..aafe1de 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "koa-compose": "^2.3.0" }, "devDependencies": { - "koa": "^0.3.0", + "koa": "^0.10.0", "should": "^3.1.2", "mocha": "^1.17.1", "supertest": "^0.9.0" diff --git a/test/lib/router.js b/test/lib/router.js index c343a3a..5b45436 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -198,6 +198,27 @@ describe('Router', function() { }); }); + it('supports custom routing detect path: ctx.routerPath', function(done) { + var app = koa(); + var router = new Router(app); + app.use(function *(next) { + // bind helloworld.example.com/users => example.com/helloworld/users + var appname = this.request.hostname.split('.', 1)[0]; + this.routerPath = '/' + appname + this.path; + yield *next; + }); + app.use(router.middleware()); + app.get('/helloworld/users', function *() { + this.body = this.method + ' ' + this.url; + }); + + request(http.createServer(app.callback())) + .get('/users') + .set('Host', 'helloworld.example.com') + .expect(200) + .expect('GET /users', done); + }); + describe('Router#[verb]()', function() { it('registers route specific to HTTP verb', function() { var app = koa();