Support non-string path in app.use(path, fn)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2207
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 4ea6f21 · Browse files · 5 files·+69 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+69 added−21 removed
- tests:+46 added−0 removed
- other:+23 added−21 removed
Provenance
- Commit
1c3bd36- Tree
d58b988- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
4ea6f21
5 files changed+69 added−21 removed
modified:lib/router/index.js+8 added-13 removed
383 unmodified lines384 * @return {app} for chaining385 * @api public386 */387 388-proto.use = function(route, fn){389- // default route to '/'390- if ('string' != typeof route) {391- fn = route;392- route = '/';388+proto.use = function(path, fn){389+ // default path to '/'390+ if (arguments.length < 2) {391+ fn = path;392+ path = '/';393 }394 395 if (typeof fn !== 'function') {396 var type = {}.toString.call(fn);397 var msg = 'Router.use() requires callback functions but got a ' + type;398 throw new Error(msg);399 }400 401- // strip trailing slash402- if ('/' == route[route.length - 1]) {403- route = route.slice(0, -1);404- }405-406- var layer = new Layer(route, {401+ var layer = new Layer(path, {402 sensitive: this.caseSensitive,408- strict: this.strict,403+ strict: false,404 end: false405 }, fn);406 407 // add the middleware413- debug('use %s %s', route || '/', fn.name || 'anonymous');408+ debug('use %s %s', path || '/', fn.name || 'anonymous');409 410 this.stack.push(layer);411 return this;412 };101 unmodified linesmodified:lib/application.js+9 added-6 removed
145 unmodified lines146 *147 * @api public148 */149 150-app.use = function(route, fn){150+app.use = function use(path, fn){151 var mount_app;152 153- // default route to '/'154- if ('string' != typeof route) fn = route, route = '/';153+ // default path to '/'154+ if (arguments.length < 2) {155+ fn = path;156+ path = '/';157+ }158 159 // express app160 if (fn.handle && fn.set) mount_app = fn;161 162 // restore .app property on req and res163 if (mount_app) {161- debug('.use app under %s', route);162- mount_app.mountpath = route;164+ debug('.use app under %s', path);165+ mount_app.mountpath = path;166 fn = function(req, res, next) {167 var orig = req.app;168 mount_app.handle(req, res, function(err) {169 req.__proto__ = orig.request;3 unmodified lines173 };174 }175 176 this.lazyrouter();174- this._router.use(route, fn);177+ this._router.use(path, fn);178 179 // mounted an app180 if (mount_app) {181 mount_app.parent = this;359 unmodified linesmodified:test/app.use.js+46 added
161 unmodified linesExpand all162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213161 unmodified linesExpand all request(app) .post('/foo/bar') .expect(200, 'saw POST /bar', cb); })
it('should support array of paths', function (done) { var app = express(); var cb = after(3, done);
app.use(['/foo/', '/bar'], function (req, res) { res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl); });
request(app) .get('/') .expect(404, cb);
request(app) .get('/foo') .expect(200, 'saw GET / through /foo', cb);
request(app) .get('/bar') .expect(200, 'saw GET / through /bar', cb); })
it('should support regexp path', function (done) { var app = express(); var cb = after(4, done);
app.use(/^\/[a-z]oo/, function (req, res) { res.send('saw ' + req.method + ' ' + req.url + ' through ' + req.originalUrl); });
request(app) .get('/') .expect(404, cb);
request(app) .get('/foo') .expect(200, 'saw GET / through /foo', cb);
request(app) .get('/zoo/bear') .expect(200, 'saw GET /bear through /zoo/bear', cb);
request(app) .get('/get/zoo') .expect(404, cb); }) })})modified:History.md+4 added
4 unmodified lines5 - previous versions are not vulnerable; this is just explicit protection6 * deprecate `res.redirect(url, status)` -- use `res.redirect(status, url)` instead7 * fix `res.send(status, num)` to send `num` as json (not error)8 * remove unnecessary escaping when `res.jsonp` returns JSON response9+ * support non-string `path` in `app.use(path, fn)`10+ - supports array of paths11+ - supports `RegExp`12 * deps: debug@1.0.313 - Add support for multiple wildcards in namespaces14 * deps: methods@1.1.015 - add `CONNECT`16 * deps: parseurl@~1.1.317 - faster parsing of href-only URLs18+ * deps: path-to-regexp@0.1.319 20 4.5.1 / 2014-07-0621 ==================22 1768 unmodified linesmodified:package.json+2 added-2 removed
32 unmodified lines33 "finalhandler": "0.0.2",34 "media-typer": "0.2.0",35 "methods": "1.1.0",36 "parseurl": "~1.1.3",37+ "path-to-regexp": "0.1.3",38 "proxy-addr": "1.0.1",39 "range-parser": "1.0.0",40 "send": "0.5.0",41 "serve-static": "~1.3.0",2 unmodified lines44 "cookie": "0.1.2",45 "fresh": "0.2.2",46 "cookie-signature": "1.0.4",47 "merge-descriptors": "0.0.2",47- "utils-merge": "1.0.0",48 "qs": "0.6.6",49- "path-to-regexp": "0.1.2"49+ "utils-merge": "1.0.0"50 },51 "devDependencies": {52 "after": "0.8.1",53 "istanbul": "0.3.0",26 unmodified lines