Skip to content
Myr

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

3 files+52 added−26 removed

modified:lib/router/index.js+26 added-24 removed
8 unmodified lines
9 , debug = require('debug')('express:router')10 , parseUrl = utils.parseUrl;1112/**13 * Expose `Router` constructor.14 */1516exports = module.exports = Router;1718/**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 */1925function Router(options) {20var proto = module.exports = function(options) {21 options = options || {};27 var self = this;2229 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 };2635 self.middleware = self.handle.bind(self);36}27 // mixin Router class functions28 router.__proto__ = proto;2930 router.params = {};31 router._params = [];32 router.caseSensitive = options.caseSensitive;33 router.strict = options.strict;34 router.stack = [];3536 return router;37};3839/**40 * Map the given param placeholder `name`(s) to the given callback.41 *42 * Parameter mapping is used to provide pre-conditions to routes
26 unmodified lines
69 * @return {app} for chaining70 * @api public71 */7272Router.prototype.param = function(name, fn){73proto.param = function(name, fn){74 // param logic75 if ('function' == typeof name) {76 this._params.push(name);77 return;
29 unmodified lines
107 *108 * @api private109 */110110Router.prototype.handle = function(req, res, done) {111proto.handle = function(req, res, done) {112 var self = this;113114 debug('dispatching %s %s', req.method, req.url);115
11 unmodified lines
127 // only used if OPTIONS request128 var options = [];129130 // middleware and routes130 var stack = this.stack;131 var stack = self.stack;132133 // for options requests, respond with a default if nothing else responds134 if (method === 'options') {135 var old = done;
106 unmodified lines
242 *243 * @api private244 */245245Router.prototype.process_params = function(route, req, res, done) {246proto.process_params = function(route, req, res, done) {247 var self = this;248 var params = this.params;249250 // captured parameters from the route, keys and values
66 unmodified lines
317 * @return {app} for chaining318 * @api public319 */320320Router.prototype.use = function(route, fn){321proto.use = function(route, fn){322323 // default route to '/'324 if ('string' != typeof route) {325 fn = route;
30 unmodified lines
356 * @return {Route}357 * @api public358 */359359Router.prototype.route = function(path){360proto.route = function(path){361 var route = new Route(path);362363 var layer = Layer(path, {364 sensitive: this.caseSensitive,
16 unmodified lines
381 * @return {app} for chaining382 * @api public383 */384384Router.prototype.all = function(path, fn) {385proto.all = function(path, fn) {386 var route = this.route(path);387 methods.forEach(function(method){388 route[method](fn);389 });390};391392// create Router#VERB functions393methods.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
modified:test/Router.js+25 added-1 removed
4 unmodified lines
5 , assert = require('assert');67describe('Router', function(){89 describe('.middleware', function(){9 it('should return a function with router methods', function() {10 var router = Router();11 assert(typeof router == 'function');1213 var router = new Router();14 assert(typeof router == 'function');1516 assert(typeof router.get == 'function');17 assert(typeof router.handle == 'function');18 assert(typeof router.use == 'function');19 });2021 it('should support .use of other routers', function(done) {22 var router = Router();23 var another = Router();2425 another.get('/bar', function(req, res) {26 res.done();27 });28 router.use('/foo', another);2930 router.handle({ url: '/foo/bar', method: 'GET' }, { done: done });31 });3233 describe('.handle', function(){34 it('should dispatch', function(done){35 var router = new Router();3637 router.route('/foo').get(function(req, res){
119 unmodified lines