Skip to content

Commit

Permalink
supports custom routing detect path: ctx.routerPath
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Aug 22, 2014
1 parent 69fed96 commit a6ce0a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 21 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a6ce0a1

Please sign in to comment.