Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only keep looping matching route middlewares if they yield next #41

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var debug = require('debug')('koa-router')
, methods = require('methods')
, parse = require('url').parse
, compose = require('koa-compose')
, Resource = require('./resource')
, Route = require('./route');

Expand Down Expand Up @@ -62,13 +63,24 @@ router.middleware = function() {
return yield next;
}

var matchingMiddlewares = [];

// Dispatch route middlewares
for (var i=0, len=matches.length; i<len; i++) {
var route = matches[i].route;
this.params = matches[i].params;
debug('dispatch "%s" %s', route.path, route.regexp);
yield route.middleware.call(this, next);
var wrapper = function(ctx, myRoute, params) {
return function *(next) {
ctx.params = params;
yield myRoute.middleware.call(ctx, next);
}
}(this, route, matches[i].params);

matchingMiddlewares.push(wrapper);
}

var fn = compose(matchingMiddlewares);
yield fn(next);
};
};

Expand Down
8 changes: 4 additions & 4 deletions test/lib/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ describe('Resource', function() {
should.exist(this.params);
this.params.should.have.property('forum', '54');
this.params.should.have.property('thread', '12');
this.status = 200;
this.status = 204;
}
});
forums.add(threads);
threads.base.should.equal('/forums/:forum/threads');
request(http.createServer(app.callback()))
.get('/forums/54/threads/12')
.expect(200)
.expect(204)
.end(function(err, res) {
if (err) return done(err);
done();
Expand All @@ -89,12 +89,12 @@ describe('Resource', function() {
app.use(router.middleware());
app.resource({
index: function *() {
this.status = 200;
this.status = 204;
}
});
request(http.createServer(app.callback()))
.get('/')
.expect(200)
.expect(204)
.end(function(err, res) {
if (err) return done(err);
done();
Expand Down
30 changes: 30 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Router', function() {
this.params.should.have.property('lastname', 'smith');
this.status = 204;
counter++;
yield next;
});
app.get('/:surname', function *(next) {
this.should.have.property('params');
Expand All @@ -87,6 +88,35 @@ describe('Router', function() {
});
});

it('matches only first route if it doesn\'t yield', function(done) {
var app = koa();
var counter = 0;
app.use(Router(app));
app.get('/:lastname', function *(next) {
this.should.have.property('params');
this.params.should.have.property('lastname', 'smith');
this.status = 204;
counter++;
});
app.get('/:surname', function *(next) {
this.should.have.property('params');
this.params.should.have.property('surname', 'smith');
this.status = 204;
counter++;

yield next;
});
var server = http.createServer(app.callback());
request(server)
.get('/smith')
.expect(204)
.end(function(err, res) {
if (err) return done(err);
counter.should.equal(1);
done();
});
});

it('no matches after throw', function(done) {
var app = koa();
var counter = 0;
Expand Down