diff --git a/.gitignore b/.gitignore index 6903f3f..d6ab776 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ logs *.log npm-debug.log* +.idea # Runtime data pids diff --git a/README.md b/README.md index 9222701..ad6d1ce 100644 --- a/README.md +++ b/README.md @@ -207,12 +207,18 @@ used to convert paths to regular expressions. <a name="module_koa-router--Router+routes"></a> -#### router.routes ⇒ <code>function</code> -Returns router middleware which dispatches a route matching the request. +#### router.routes(name) ⇒ <code>function</code> +Returns router middleware which dispatches a route matching the request, +name is optional **Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code> <a name="module_koa-router--Router+use"></a> +| Param | Type | +| --- | --- | +| name | <code>String</code> | + + #### router.use([path], middleware) ⇒ <code>Router</code> Use given middleware. diff --git a/lib/router.js b/lib/router.js index fe1ef44..56f57e5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -311,49 +311,42 @@ Router.prototype.prefix = function (prefix) { * @returns {Function} */ -Router.prototype.routes = Router.prototype.middleware = function () { - var router = this; - - var dispatch = function dispatch(ctx, next) { - debug('%s %s', ctx.method, ctx.path); - - var path = router.opts.routerPath || ctx.routerPath || ctx.path; - var matched = router.match(path, ctx.method); - var layerChain, layer, i; - - if (ctx.matched) { - ctx.matched.push.apply(ctx.matched, matched.path); - } else { - ctx.matched = matched.path; - } - - ctx.router = router; - - if (!matched.route) return next(); - - var matchedLayers = matched.pathAndMethod - var mostSpecificLayer = matchedLayers[matchedLayers.length - 1] - ctx._matchedRoute = mostSpecificLayer.path; - if (mostSpecificLayer.name) { - ctx._matchedRouteName = mostSpecificLayer.name; - } - - layerChain = matchedLayers.reduce(function(memo, layer) { - memo.push(function(ctx, next) { - ctx.captures = layer.captures(path, ctx.captures); - ctx.params = layer.params(path, ctx.captures, ctx.params); - ctx.routerName = layer.name; - return next(); - }); - return memo.concat(layer.stack); - }, []); - - return compose(layerChain)(ctx, next); - }; - - dispatch.router = this; - - return dispatch; +Router.prototype.routes = Router.prototype.middleware = function (name) { + var router = this; + var middlewareFnName = name || 'dispatch'; + var fn = { + [middlewareFnName]: (ctx, next) => { + debug('%s %s', ctx.method, ctx.path); + var path = router.opts.routerPath || ctx.routerPath || ctx.path; + var matched = router.match(path, ctx.method); + var layerChain, layer, i; + if (ctx.matched) { + ctx.matched.push.apply(ctx.matched, matched.path); + } else { + ctx.matched = matched.path; + } + ctx.router = router; + if (!matched.route) return next(); + var matchedLayers = matched.pathAndMethod; + var mostSpecificLayer = matchedLayers[matchedLayers.length - 1]; + ctx._matchedRoute = mostSpecificLayer.path; + if (mostSpecificLayer.name) { + ctx._matchedRouteName = mostSpecificLayer.name; + } + layerChain = matchedLayers.reduce(function (memo, layer) { + memo.push(function (ctx, next) { + ctx.captures = layer.captures(path, ctx.captures); + ctx.params = layer.params(path, ctx.captures, ctx.params); + ctx.routerName = layer.name; + return next(); + }); + return memo.concat(layer.stack); + }, []); + return compose(layerChain)(ctx, next); + } + }[middlewareFnName]; + fn.router = this; + return fn; }; /** diff --git a/test/lib/router.js b/test/lib/router.js index 44e7112..28470d6 100644 --- a/test/lib/router.js +++ b/test/lib/router.js @@ -15,13 +15,28 @@ var fs = require('fs') describe('Router', function () { it('creates new router with koa app', function (done) { - var app = new Koa(); var router = new Router(); router.should.be.instanceOf(Router); done(); }); - it('shares context between routers (gh-205)', function (done) { + it('creates new router with name', function (done) { + var router = new Router(); + var middlewareName = 'test_middleware'; + var middleware = router.routes(middlewareName); + expect(middleware.name).to.be(middlewareName); + done(); + }); + + it('creates new router with default name', function (done) { + var router = new Router(); + var defaultMiddlewareName = 'dispatch'; + var middleware = router.routes(); + expect(middleware.name).to.be(defaultMiddlewareName); + done(); + }); + + it('shares context between routers (gh-205)', function (done) { var app = new Koa(); var router1 = new Router(); var router2 = new Router();