Added "strict routing" setting for trailing slashes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
for people who are: a) over-optimizing seo b) working on fs-like APIs
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 4b9cc3d · Browse files · 4 files·+35 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+35 added−6 removed
- tests:+23 added−1 removed
- other:+12 added−5 removed
Provenance
- Commit
6f7075b- Tree
8a5081b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
4b9cc3d
4 files changed+35 added−6 removed
modified:docs/guide.md+1 added
102 unmodified lines103 * _view engine_ Default view engine name for views rendered without extensions104 * _view options_ An object specifying global view options105 * _view cache_ Enable view caching (enabled in production)106 * _case sensitive routes_ Enable case-sensitive routing107+ * _strict routing_ When enabled trailing slashes are no longer ignored108 109 ### Routing110 111 Express utilizes the HTTP verbs to provide a meaningful, expressive routing API.1296 unmodified linesmodified:lib/router/index.js+1 added
371 unmodified lines372 373 // create the route374 var route = new Route(method, path, fn, {375 sensitive: app.enabled('case sensitive routes')376+ , strict: app.enabled('strict routing')377 , middleware: middleware378 });379 380 // add it4 unmodified linesmodified:lib/router/route.js+10 added-5 removed
15 unmodified lines16171819202021222324256 unmodified lines323334353536373839404142434418 unmodified lines636465666768697071676872737470757677787911 unmodified lines15 unmodified lines * and callback `fn` and `options`. * * Options: * * - `sensitive` enable case-sensitive routes * - `sensitive` enable case-sensitive routes * - `strict` enable strict matching for trailing slashes * - `middleware` array of middleware * * @param {String} method * @param {String} path6 unmodified lines options = options || {}; this.callback = fn; this.path = path; this.method = method; this.regexp = normalize(path, this.keys = [], options.sensitive); this.middleware = options.middleware; this.regexp = normalize(path , this.keys = [] , options.sensitive , options.strict);}
/** * Check if this route matches `path` and return captures made.18 unmodified lines * * @param {String|RegExp} path * @param {Array} keys * @param {Boolean} sensitive * @param {Boolean} strict * @return {RegExp} * @api private */
function normalize(path, keys, sensitive) { if (path instanceof RegExp) return path; function normalize(path, keys, sensitive, strict) { if (path instanceof RegExp) return path; path = path .concat('/?') .concat(strict ? '' : '/?') .replace(/\/\(/g, '(?:/') .replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){ keys.push({ name: key, optional: !! optional }); slash = slash || '';11 unmodified linesmodified:test/router.test.js+23 added-1 removed
315 unmodified lines316 ret.should.have.length(3);317 app.match.all('/user/12').should.have.length(0);318 app.get('/user/:id').should.have.length(0);319 },320- 320+321+ 'test "strict routing" setting': function(){322+ var app = express.createServer();323+324+ app.enable('strict routing');325+326+ app.get('/:path', function(req, res, next){327+ res.send({ type: 'directory' });328+ });329+330+ app.get('/:path/', function(req, res, next){331+ res.send(['.', '..', 'foo.js', 'bar.js']);332+ });333+334+ assert.response(app,335+ { url: '/lib' },336+ { body: '{"type":"directory"}' });337+338+ assert.response(app,339+ { url: '/lib/' },340+ { body: '[".","..","foo.js","bar.js"]' });341+ },342+343 'test "case sensitive routes" setting': function(){344 var app = express.createServer();345 346 app.enable('case sensitive routes');61 unmodified lines