diff --git a/README.md b/README.md index d6295011..b7b22e9e 100644 --- a/README.md +++ b/README.md @@ -306,8 +306,11 @@ So if you declare the following routes and the URL of the incoming request is /33/foo/bar, the second route will be matched because the first chunk (33) matches the static chunk. If the URL would have been /32/foo/bar, the first route would have been matched. +Once a url has been matched, `find-my-way` will figure out which handler registered for that path matches the request if there are any constraints. +`find-my-way` will check the most constrained handlers first, which means the handlers with the most keys in the `constraints` object. -Once a url has been matched, `find-my-way` will figure out which handler registered for that path matches the request if there are any constraints. `find-my-way` will check the most constrained handlers first, which means the handlers with the most keys in the `constraints` object. +> If you just want a path containing a colon without declaring a parameter, use a double colon. +> For example, `/name::customVerb` will be interpreted as `/name:customVerb` ##### Supported methods diff --git a/index.js b/index.js index 6f965121..ee3f1abe 100644 --- a/index.js +++ b/index.js @@ -120,6 +120,13 @@ Router.prototype._on = function _on (method, path, opts, handler, store) { // search for parametric or wildcard routes // parametric route if (path.charCodeAt(i) === 58) { + if (i !== len - 1 && path.charCodeAt(i + 1) === 58) { + // It's a double colon. Let's just replace it with a single colon and go ahead + path = path.slice(0, i) + path.slice(i + 1) + len = path.length + continue + } + var nodeType = NODE_TYPES.PARAM j = i + 1 var staticPart = path.slice(0, i) diff --git a/test/issue-175.test.js b/test/issue-175.test.js new file mode 100644 index 00000000..17b55cbd --- /dev/null +++ b/test/issue-175.test.js @@ -0,0 +1,31 @@ +'use strict' + +const t = require('tap') +const test = t.test +const FindMyWay = require('..') + +test('double colon is replaced with single colon, no parameters', t => { + t.plan(1) + const findMyWay = FindMyWay({ + defaultRoute: () => t.fail('should not be default route') + }) + + function handler (req, res, params) { + t.deepEqual(params, {}) + } + + findMyWay.on('GET', '/name::customVerb', handler) + + findMyWay.lookup({ method: 'GET', url: '/name:customVerb' }, null) +}) + +test('exactly one match for static route with colon', t => { + t.plan(2) + const findMyWay = FindMyWay() + + function handler () {} + findMyWay.on('GET', '/name::customVerb', handler) + + t.equal(findMyWay.find('GET', '/name:customVerb').handler, handler) + t.equal(findMyWay.find('GET', '/name:test'), null) +})