Issue 944:
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Changing Router#match() signature from (req[, i]) to (method, url[, i]), and making it public
authored by riadhchtara · committed by riadhchtara · parent 136f054 · Browse files · 2 files·+51 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+51 added−8 removed
- tests:+30 added−5 removed
- other:+21 added−3 removed
Provenance
- Commit
f110248- Tree
cbbc40c- Author
riadhchtara <riadh.chtara@gmail.com>- Committer
riadhchtara <riadh.chtara@gmail.com>- Parent
136f054
2 files changed+51 added−8 removed
modified:lib/router/index.js+21 added-3 removed
101 unmodified lines102 pass(req._route_index + 1, err);103 }104 105 // match route106- req.route = route = self.match(req, i);106+ req.route = route = self.matchReq(req, i);107 108 // implied OPTIONS109 if (!route && 'OPTIONS' == req.method) return self._options(req, res);110 108 unmodified lines219 * @return {Route}220 * @api private221 */222 223-Router.prototype.match = function(req, i, head){223+Router.prototype.matchReq = function(req, i, head){224 var method = req.method.toLowerCase()225 , url = parse(req)226 , path = url.pathname227 , routes = this.map1 unmodified line229 , route;230 231 // HEAD support232 if (!head && 'head' == method) {233- route = this.match(req, i, true);233+ route = this.matchReq(req, i, true);234 if (route) return route;235 method = 'get';236 }237 11 unmodified lines249 }250 };251 252 /**253+ * Attempt to match a route for `method`254+ * and `url` with optional starting255+ * index of `i` defaulting to 0.256+ *257+ * @param {String} method258+ * @param {String} url259+ * @param {Number} i260+ * @return {Route}261+ * @api public262+ */263+264+Router.prototype.match = function(method, url, i, head){265+ method = method.toLowerCase(); 266+ var req = { method: method, url: url };267+ return this.matchReq(req, i, head);268+};269+270+/**271 * Route `method`, `path`, and one or more callbacks.272 *273 * @param {String} method274 * @param {String} path23 unmodified linesmodified:test/Router.js+30 added-5 removed
10 unmodified lines111213141515161718192021222324252627282930313233343536373839404142434445462247484950512752535455315657585935606162636414 unmodified lines10 unmodified lines router = new Router; app = express(); })
describe('.match(req, i)', function(){ describe('.match(method, url, i)', function(){ it('should match based on index', function(){ router.route('get', '/foo', function(){}); router.route('get', '/foob?', function(){}); router.route('get', '/bar', function(){}); var method = 'GET' , url = '/foo?bar=baz';
var route = router.match(method, url, 0); route.constructor.name.should.equal('Route'); route.method.should.equal('get'); route.path.should.equal('/foo');
var route = router.matchRe(method, url, 1); route.path.should.equal('/foob?');
var route = router.match(method, url, 2); assert(!route);
req.url = '/bar'; var route = router.match(method, url); route.path.should.equal('/bar'); }) }) describe('.matchReq(req, i)', function(){ it('should match based on index', function(){ router.route('get', '/foo', function(){}); router.route('get', '/foob?', function(){}); router.route('get', '/bar', function(){}); var req = { method: 'GET', url: '/foo?bar=baz' };
var route = router.match(req, 0); var route = router.matchReq(req, 0); route.constructor.name.should.equal('Route'); route.method.should.equal('get'); route.path.should.equal('/foo');
var route = router.match(req, 1); var route = router.matchReq(req, 1); req._route_index.should.equal(1); route.path.should.equal('/foob?');
var route = router.match(req, 2); var route = router.matchReq(req, 2); assert(!route);
req.url = '/bar'; var route = router.match(req); var route = router.matchReq(req); route.path.should.equal('/bar'); }) })
14 unmodified lines