Skip to content

Commit

Permalink
Merge branch 'pull'
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Mar 15, 2010
2 parents c429e88 + 3aa870d commit f008af0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
30 changes: 22 additions & 8 deletions lib/express/core.js
Expand Up @@ -104,10 +104,16 @@ Router = Class({
* @api private
*/

route: function(){
var route = this.matchingRoute()
if (route)
return route.fn.apply(this.request, this.request.captures.slice(1))
route: function() {
var route = this.matchingRoute(), body
if (route) {
body = route.fn.apply(this.request, this.request.captures.slice(1));
if (this.request.passed) {
this.request.passed = false
return this.route()
}
return body;
}
else if (this.request.accepts('html') && set('helpful 404'))
this.request.halt(404, require('express/pages/not-found').render(this.request))
else
Expand All @@ -121,10 +127,18 @@ Router = Class({
* @api private
*/

matchingRoute: function(){
return Express.routes.find(function(route){
return this.match(route)
}, this)
matchingRoute: function() {
// Stop at the first match. If request.pass() was called
// continue from where we left off
var i = -1
this.matchingRoute = function() {
var routes = Express.routes, route
while (route = routes[++i])
if (this.match(route))
break
return route
}
return this.matchingRoute()
},

/**
Expand Down
11 changes: 11 additions & 0 deletions lib/express/request.js
Expand Up @@ -178,6 +178,17 @@ exports.Request = Class({
});
},

/**
* Pass control to the next matching route.
*
* @api public
*/

pass: function () {
this.passed = true
return this
},

/**
* Set Content-Type header to the mime type
* for the given _path_, which calls mime.type().
Expand Down
14 changes: 14 additions & 0 deletions spec/spec.request.js
Expand Up @@ -198,6 +198,20 @@ describe 'Express'
})
get('/public/app.js').body.should.eql 'public, app, js'
end
end

describe '#pass()'
it 'should pass control to the next matching route'
get('/user', function () {
this.pass()
})
get('/user', function () {
this.pass()
return 'nodejs'
})
get('/user', function () { return 'success'})
get('/user').body.should.eql 'success'
end
end
end
end

0 comments on commit f008af0

Please sign in to comment.