diff --git a/index.js b/index.js index 484cf7ff..e4387cc0 100644 --- a/index.js +++ b/index.js @@ -421,7 +421,8 @@ Router.prototype.find = function find (method, path, version) { return this._getWildcardNode(wildcardNode, method, originalPath, pathLenWildcard) } - if (originalPath.indexOf('/' + previousPath) === -1) { + var goBack = this.ignoreTrailingSlash ? previousPath : '/' + previousPath + if (originalPath.indexOf(goBack) === -1) { // we need to know the outstanding path so far from the originalPath since the last encountered "/" and assign it to previousPath. // e.g originalPath: /aa/bbb/cc, path: bb/cc // outstanding path: /bbb/cc diff --git a/test/issue-145.test.js b/test/issue-145.test.js new file mode 100644 index 00000000..8641feec --- /dev/null +++ b/test/issue-145.test.js @@ -0,0 +1,24 @@ +'use strict' + +const t = require('tap') +const FindMyWay = require('../') + +t.test('issue-145', (t) => { + t.plan(8) + + const findMyWay = FindMyWay({ ignoreTrailingSlash: true }) + + const fixedPath = function staticPath () {} + const varPath = function parameterPath () {} + findMyWay.on('GET', '/a/b', fixedPath) + findMyWay.on('GET', '/a/:pam/c', varPath) + + t.equals(findMyWay.find('GET', '/a/b').handler, fixedPath) + t.equals(findMyWay.find('GET', '/a/b/').handler, fixedPath) + t.equals(findMyWay.find('GET', '/a/b/c').handler, varPath) + t.equals(findMyWay.find('GET', '/a/b/c/').handler, varPath) + t.equals(findMyWay.find('GET', '/a/foo/c').handler, varPath) + t.equals(findMyWay.find('GET', '/a/foo/c/').handler, varPath) + t.notOk(findMyWay.find('GET', '/a/c')) + t.notOk(findMyWay.find('GET', '/a/c/')) +})