Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add name param to route.routes() #477

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
logs
*.log
npm-debug.log*
.idea

# Runtime data
pids
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
79 changes: 36 additions & 43 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
19 changes: 17 additions & 2 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down