diff --git a/index.js b/index.js index 362bccd..595f7b4 100644 --- a/index.js +++ b/index.js @@ -233,20 +233,19 @@ Router.prototype.handle = function handle(req, res, callback) { // find next matching layer var layer - var match + var match = null var route - while (match !== true && idx < stack.length) { + while (!match && idx < stack.length) { layer = stack[idx++] match = matchLayer(layer, path) - route = layer.route - - if (typeof match !== 'boolean') { + if (match && typeof match.path !== 'string') { // hold on to layerError layerError = layerError || match } + route = layer.route - if (match !== true) { + if (!match) { continue } @@ -257,7 +256,7 @@ Router.prototype.handle = function handle(req, res, callback) { if (layerError) { // routes do not match with a pending error - match = false + match = null continue } @@ -271,13 +270,13 @@ Router.prototype.handle = function handle(req, res, callback) { // don't even bother matching route if (!has_method && method !== 'HEAD') { - match = false + match = null continue } } // no match - if (match !== true) { + if (!match) { return done(layerError) } @@ -288,9 +287,9 @@ Router.prototype.handle = function handle(req, res, callback) { // Capture one-time layer values req.params = self.mergeParams - ? mergeParams(layer.params, parentParams) - : layer.params - var layerPath = layer.path + ? mergeParams(match.params, parentParams) + : match.params + var layerPath = match.path // this should be done for the layer self.process_params(layer, paramcalled, req, res, function (err) { diff --git a/lib/layer.js b/lib/layer.js index 430f595..056cbae 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -38,8 +38,6 @@ function Layer(path, options, fn) { this.handle = fn this.name = fn.name || '' - this.params = undefined - this.path = undefined this.regexp = pathRegexp(path, this.keys = [], opts) // set fast path flags @@ -111,16 +109,18 @@ Layer.prototype.match = function match(path) { if (path != null) { // fast path non-ending match for / (any path matches) if (this.regexp.fast_slash) { - this.params = {} - this.path = '' - return true + return { + path: '', + params: {} + } } // fast path for * (everything matched in a param) if (this.regexp.fast_star) { - this.params = {'0': decode_param(path)} - this.path = path - return true + return { + path: path, + params: {'0': decode_param(path)} + } } // match the path @@ -128,18 +128,12 @@ Layer.prototype.match = function match(path) { } if (!match) { - this.params = undefined - this.path = undefined - return false + return null } - // store values - this.params = {} - this.path = match[0] - // iterate matches var keys = this.keys - var params = this.params + var params = {} for (var i = 1; i < match.length; i++) { var key = keys[i - 1] @@ -151,7 +145,10 @@ Layer.prototype.match = function match(path) { } } - return true + return { + path: match[0], + params: params + } } /** diff --git a/test/req.params.js b/test/req.params.js index fc0e16c..70882a0 100644 --- a/test/req.params.js +++ b/test/req.params.js @@ -3,6 +3,7 @@ var Router = require('..') var utils = require('./support/utils') var createServer = utils.createServer +var assert = utils.assert var request = utils.request describe('req.params', function () { @@ -67,6 +68,27 @@ describe('req.params', function () { .expect(200, '{"foo":"bar"}', done) }) + it('should not keep parameters in memory', function (done) { + var router = Router() + var server = createServer(function (req, res, next) { + router(req, res, function (err) { + if (err) return next(err) + sawParams(req, res) + }) + }) + + router.get('/:fizz', hitParams(1)) + + request(server) + .get('/buzz') + .end(function(err) { + if (err) return done(err) + + assert.strictEqual(router.stack[0].params, undefined) + done() + }) + }) + describe('when "mergeParams: true"', function () { it('should merge outside object with params', function (done) { var router = Router({ mergeParams: true })