make express.Router() return a Router function instance
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Similar to how express() returns an express `app` instance which is also a function, express.Router() returns the Router instance which is also a function and can be easily used via another router or the app. app.use(express.Router());
authored by Roman Shtylman · committed by Roman Shtylman · parent caa25b5 · Browse files · 3 files·+52 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+52 added−26 removed
- tests:+26 added−2 removed
- other:+26 added−24 removed
Provenance
- Commit
f8b954b- Tree
15b2a57- Author
Roman Shtylman <shtylman@gmail.com>- Committer
Roman Shtylman <shtylman@gmail.com>- Parent
caa25b5
3 files changed+52 added−26 removed
modified:lib/router/index.js+26 added-24 removed
8 unmodified lines9 , debug = require('debug')('express:router')10 , parseUrl = utils.parseUrl;11 12 /**13- * Expose `Router` constructor.14- */15-16-exports = module.exports = Router;17-18-/**13 * Initialize a new `Router` with the given `options`.14 *15 * @param {Object} options22- * @api private16+ * @return {Router} which is an callable function17+ * @api public18 */19 25-function Router(options) {20+var proto = module.exports = function(options) {21 options = options || {};27- var self = this;22 29- self.params = {};30- self._params = [];31- self.caseSensitive = options.caseSensitive;32- self.strict = options.strict;33- self.stack = [];23+ function router(req, res, next) {24+ router.handle(req, res, next);25+ };26 35- self.middleware = self.handle.bind(self);36-}27+ // mixin Router class functions28+ router.__proto__ = proto;29 30+ router.params = {};31+ router._params = [];32+ router.caseSensitive = options.caseSensitive;33+ router.strict = options.strict;34+ router.stack = [];35+36+ return router;37+};38+39 /**40 * Map the given param placeholder `name`(s) to the given callback.41 *42 * Parameter mapping is used to provide pre-conditions to routes26 unmodified lines69 * @return {app} for chaining70 * @api public71 */72 72-Router.prototype.param = function(name, fn){73+proto.param = function(name, fn){74 // param logic75 if ('function' == typeof name) {76 this._params.push(name);77 return;29 unmodified lines107 *108 * @api private109 */110 110-Router.prototype.handle = function(req, res, done) {111+proto.handle = function(req, res, done) {112 var self = this;113 114 debug('dispatching %s %s', req.method, req.url);115 11 unmodified lines127 // only used if OPTIONS request128 var options = [];129 130 // middleware and routes130- var stack = this.stack;131+ var stack = self.stack;132 133 // for options requests, respond with a default if nothing else responds134 if (method === 'options') {135 var old = done;106 unmodified lines242 *243 * @api private244 */245 245-Router.prototype.process_params = function(route, req, res, done) {246+proto.process_params = function(route, req, res, done) {247 var self = this;248 var params = this.params;249 250 // captured parameters from the route, keys and values66 unmodified lines317 * @return {app} for chaining318 * @api public319 */320 320-Router.prototype.use = function(route, fn){321+proto.use = function(route, fn){322 323 // default route to '/'324 if ('string' != typeof route) {325 fn = route;30 unmodified lines356 * @return {Route}357 * @api public358 */359 359-Router.prototype.route = function(path){360+proto.route = function(path){361 var route = new Route(path);362 363 var layer = Layer(path, {364 sensitive: this.caseSensitive,16 unmodified lines381 * @return {app} for chaining382 * @api public383 */384 384-Router.prototype.all = function(path, fn) {385+proto.all = function(path, fn) {386 var route = this.route(path);387 methods.forEach(function(method){388 route[method](fn);389 });390 };391 392 // create Router#VERB functions393 methods.forEach(function(method){393- Router.prototype[method] = function(path, fn){394+ proto[method] = function(path, fn){395 var self = this;396 self.route(path)[method](fn);397 return self;398 };399 });400+modified:test/app.options.js+1 added-1 removed
29 unmodified lines3031323334343536373823 unmodified lines29 unmodified lines var app = express(); var router = new express.Router();
router.get('/users', function(req, res){}); app.use(router.middleware); app.use(router); app.get('/other', function(req, res){});
request(app) .options('/other')23 unmodified linesmodified:test/Router.js+25 added-1 removed
4 unmodified lines5 , assert = require('assert');6 7 describe('Router', function(){8 9- describe('.middleware', function(){9+ it('should return a function with router methods', function() {10+ var router = Router();11+ assert(typeof router == 'function');12+13+ var router = new Router();14+ assert(typeof router == 'function');15+16+ assert(typeof router.get == 'function');17+ assert(typeof router.handle == 'function');18+ assert(typeof router.use == 'function');19+ });20+21+ it('should support .use of other routers', function(done) {22+ var router = Router();23+ var another = Router();24+25+ another.get('/bar', function(req, res) {26+ res.done();27+ });28+ router.use('/foo', another);29+30+ router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });31+ });32+33+ describe('.handle', function(){34 it('should dispatch', function(done){35 var router = new Router();36 37 router.route('/foo').get(function(req, res){119 unmodified lines