Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: koajs/router
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: eac80a3b2fb8ff73fcd4cc88864c8726877be290
Choose a base ref
...
head repository: koajs/router
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 81dc60a6d2eff3cf0f8dec70f3f49e6fff0f1f03
Choose a head ref

Commits on Nov 19, 2015

  1. refactor for koa2.x, use compose for stacks

    * update koa dependency to alpha2
    * add koa-compose dependency for layer stack composition
    * update middleware signature to `(ctx, next)`
    * update param middleware signature to `(param, ctx, next)`
    * remove all generator functions and gen type checks
    * update all specs for promise-based usage
    * updates examples / comments / docs
    jbielick committed Nov 19, 2015

    Verified

    This commit was signed with the committer’s verified signature.
    bagder Daniel Stenberg
    Copy the full SHA
    9e742f1 View commit details
  2. change travis build to 4, 5

    jbielick committed Nov 19, 2015
    Copy the full SHA
    9179a70 View commit details

Commits on Nov 21, 2015

  1. Require node >= 4

    alexmingoia committed Nov 21, 2015
    Copy the full SHA
    0f20f36 View commit details

Commits on Nov 22, 2015

  1. 6.0.0

    alexmingoia committed Nov 22, 2015
    Copy the full SHA
    f836f1f View commit details

Commits on Nov 24, 2015

  1. fixes ZijianHe/koa-router#208

    Delyan Haralanov committed Nov 24, 2015
    Copy the full SHA
    6a970fb View commit details
  2. Copy the full SHA
    bce2e6d View commit details
  3. Copy the full SHA
    7519239 View commit details
  4. 6.0.1

    alexmingoia committed Nov 24, 2015
    Copy the full SHA
    9ae3b46 View commit details
  5. Copy the full SHA
    d6591e3 View commit details
  6. Copy the full SHA
    05f52a6 View commit details
  7. 6.1.0

    alexmingoia committed Nov 24, 2015
    Copy the full SHA
    75070f8 View commit details
  8. Copy the full SHA
    08820ef View commit details
  9. 6.2.0

    alexmingoia committed Nov 24, 2015
    Copy the full SHA
    faab16d View commit details

Commits on Nov 25, 2015

  1. Copy the full SHA
    f1b01d6 View commit details
  2. Always run middleware in order defined by .use().

    This was done to match the Express 4 API, and to reduce confusion and complexity
    with regards to middleware sequence. Closes #194.
    alexmingoia committed Nov 25, 2015
    Copy the full SHA
    8665620 View commit details
  3. Register multiple routes with array of paths.

    Closes #203.
    alexmingoia committed Nov 25, 2015
    Copy the full SHA
    dde14ae View commit details
  4. Copy the full SHA
    20e9c04 View commit details
  5. 7.0.0

    alexmingoia committed Nov 25, 2015
    Copy the full SHA
    b8998e1 View commit details

Commits on Nov 26, 2015

  1. Copy the full SHA
    1f1c45e View commit details
  2. Update readme.

    alexmingoia committed Nov 26, 2015
    Copy the full SHA
    76f6337 View commit details

Commits on Dec 12, 2015

  1. Copy the full SHA
    09d6969 View commit details

Commits on Jan 5, 2016

  1. Copy the full SHA
    dd2f0ee View commit details

Commits on Jan 19, 2016

  1. Copy the full SHA
    a0e0dbd View commit details
  2. Fix typo.

    alexmingoia committed Jan 19, 2016
    Copy the full SHA
    eec15be View commit details
  3. Copy the full SHA
    17fd098 View commit details

Commits on Jan 20, 2016

  1. 7.0.1

    alexmingoia committed Jan 20, 2016
    Copy the full SHA
    ed6a21d View commit details

Commits on Feb 12, 2016

  1. update readme to reflect next() promise return

     #234 points out that the example does not yield to the next middleware
     when adding additional and returning a promise. The `next` argument
     must be resolved and added to the promise chain in order for middleware
     execution to continue.
    
     ex:
    
    ```
     router.use(function(ctx, next) {
       return new Promise(function(resolve, reject) {
         setTimeout(function() {
           // must resolve( { the next middleware } )
           // to continue the promise chain.
           resolve(next());
         }, 100);
       });
     }, function(ctx, next) {
       // will now be called
     });
    ```
    jbielick committed Feb 12, 2016
    Copy the full SHA
    7ff98ae View commit details
  2. make test examples async for koa2-like usage

    Existing test examples mounting multiple middleware weren't async, which
    didn't offer a good picture of how to use the router and multiple
    middlware with the next `ctx, next` koa2 middlware arguments.
    
    this establishes a working async example in two of the tests -- with
    an isolated test case showing multiple middlware mounted for one route
    jbielick committed Feb 12, 2016
    Copy the full SHA
    2ee7cc7 View commit details
  3. Merge pull request #242 from alexmingoia/koa2-async-tests

    make test examples async for koa2-like usage
    alexmingoia committed Feb 12, 2016
    Copy the full SHA
    90f7c67 View commit details

Commits on Sep 2, 2016

  1. fix incorrect params with router.use(fn)

    fixes #247 for koa2 (master branch)
    aheckmann committed Sep 2, 2016
    Copy the full SHA
    19e60d9 View commit details

Commits on Sep 3, 2016

  1. Merge pull request #293 from aheckmann/fix_params_with_use_koa2

    fix incorrect params with router.use(fn) (koa2)
    jbielick authored Sep 3, 2016
    Copy the full SHA
    72c31fa View commit details

Commits on Sep 29, 2016

  1. Copy the full SHA
    b1b5542 View commit details
  2. doc: fix typo

    haoxin authored and jbielick committed Sep 29, 2016
    Copy the full SHA
    0b6c113 View commit details
  3. Copy the full SHA
    d25bcf3 View commit details
  4. Skip failing test pending patch.

    alexmingoia authored and jbielick committed Sep 29, 2016
    Copy the full SHA
    8984212 View commit details
  5. add _matchedRoute to context

    jergason authored and jbielick committed Sep 29, 2016
    Copy the full SHA
    844232f View commit details
  6. Copy the full SHA
    58c7dfb View commit details
  7. 5.4.0

    alexmingoia authored and jbielick committed Sep 29, 2016
    Copy the full SHA
    16ed8b4 View commit details
  8. 5.4 backports for 7.0 mainline

    - merge in the 5.4 fixes and updates to 7.0
    - update backports and tests
    jbielick committed Sep 29, 2016
    Copy the full SHA
    ca5be1d View commit details
  9. 7.1

    jbielick committed Sep 29, 2016
    Copy the full SHA
    865c27d View commit details
  10. [ci] build on node 6

    jbielick committed Sep 29, 2016
    Copy the full SHA
    3b7620a View commit details

Commits on Sep 30, 2016

  1. Merge branch '5.x'

    jbielick committed Sep 30, 2016
    Copy the full SHA
    ac9dbea View commit details

Commits on Dec 11, 2016

  1. add node 5,6,7 to CI build

    jbielick committed Dec 11, 2016
    Copy the full SHA
    abc8217 View commit details

Commits on Jan 5, 2017

  1. Copy the full SHA
    825dbe4 View commit details

Commits on Jan 8, 2017

  1. Merge pull request #322 from zzuieliyaoli/master

    make README.md more clear
    jbielick authored Jan 8, 2017
    Copy the full SHA
    1135f78 View commit details

Commits on Jan 17, 2017

  1. Adding documentation around Router#all

    This is a useful feature that should be documented.
    pschwyter authored Jan 17, 2017
    Copy the full SHA
    c133a51 View commit details

Commits on Jan 30, 2017

  1. Copy the full SHA
    d75a165 View commit details

Commits on Mar 15, 2017

  1. add _matchedRouteName to context

    sometimes route paths can be very long & convoluted, exposing the route
    name provides a saner alternative analytics
    Aditya committed Mar 15, 2017
    Copy the full SHA
    2969bc0 View commit details
  2. Merge pull request #326 from pschwyter/patch-2

    Adding documentation around Router#all
    jbielick authored Mar 15, 2017
    Copy the full SHA
    4411ab2 View commit details
  3. broken link

    nothing much to it.
    CreativeCactus authored and jbielick committed Mar 15, 2017
    Copy the full SHA
    50002d2 View commit details
Showing with 1,187 additions and 867 deletions.
  1. +3 −5 .travis.yml
  2. +156 −92 README.md
  3. +36 −1 history.md
  4. +11 −2 lib/README_tpl.hbs
  5. +33 −21 lib/layer.js
  6. +187 −164 lib/router.js
  7. +9 −7 package.json
  8. +76 −76 test/lib/layer.js
  9. +676 −499 test/lib/router.js
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
language: node_js
node_js:
- "4"
- "3"
- "2"
- "1"
- "0.12"
- "6"
- "7"
- "8"
notifications:
email:
on_success: never
248 changes: 156 additions & 92 deletions README.md

Large diffs are not rendered by default.

37 changes: 36 additions & 1 deletion history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
# History

## 7.3.0

- Router#url() now accepts query parameters to add to generated urls [#396](https://github.com/alexmingoia/koa-router/pull/396)

## 7.2.1

- Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)

## 7.2.0

- Fix a bug in Router#url and append Router object to ctx. [#350](https://github.com/alexmingoia/koa-router/pull/350)
- Adds `_matchedRouteName` to context [#337](https://github.com/alexmingoia/koa-router/pull/337)
- Respond to CORS preflights with 200, 0 length body [#359](https://github.com/alexmingoia/koa-router/issues/359)

## 7.1.1

- Fix bug where param handlers were run out of order [#282](https://github.com/alexmingoia/koa-router/pull/282)

## 7.1.0

- Backports: merge 5.4 work into the 7.x upstream. See 5.4.0 updates for more details.

## 7.0.1

- Fix: allowedMethods should be ctx.method not this.method [#215](https://github.com/alexmingoia/koa-router/pull/215)

## 7.0.0

- The API has changed to match the new promise-based middleware
signature of koa 2. See the
[koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
information.
- Middleware is now always run in the order declared by `.use()` (or `.get()`,
etc.), which matches Express 4 API.

## 5.4.0

- Expose matched route at `ctx._matchedRoute`.
@@ -9,7 +44,7 @@
- Register multiple routes with array of paths [#203](https://github.com/alexmingoia/koa-router/issue/143).
- Improved router.url() [#143](https://github.com/alexmingoia/koa-router/pull/143)
- Adds support for named routes and regular expressions
[#152](https://github.com/alexmingoia/koa-router/pulls/152)
[#152](https://github.com/alexmingoia/koa-router/pull/152)
- Add support for custom throw functions for 405 and 501 responses [#206](https://github.com/alexmingoia/koa-router/pull/206)

## 5.2.3
13 changes: 11 additions & 2 deletions lib/README_tpl.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# koa-router

[![NPM version](http://img.shields.io/npm/v/koa-router.svg?style=flat)](https://npmjs.org/package/koa-router) [![NPM Downloads](https://img.shields.io/npm/dm/koa-router.svg?style=flat)](https://npmjs.org/package/koa-router) [![Node.js Version](https://img.shields.io/node/v/koa-router.svg?style=flat)](http://nodejs.org/download/) [![Build Status](http://img.shields.io/travis/alexmingoia/koa-router.svg?style=flat)](http://travis-ci.org/alexmingoia/koa-router) [![Tips](https://img.shields.io/gratipay/alexmingoia.svg?style=flat)](https://www.gratipay.com/alexmingoia/) [![Gitter Chat](https://img.shields.io/badge/gitter-join%20chat-1dce73.svg?style=flat)](https://gitter.im/alexmingoia/koa-router/)
[![NPM version](https://img.shields.io/npm/v/koa-router.svg?style=flat)](https://npmjs.org/package/koa-router) [![NPM Downloads](https://img.shields.io/npm/dm/koa-router.svg?style=flat)](https://npmjs.org/package/koa-router) [![Node.js Version](https://img.shields.io/node/v/koa-router.svg?style=flat)](http://nodejs.org/download/) [![Build Status](https://img.shields.io/travis/alexmingoia/koa-router.svg?style=flat)](http://travis-ci.org/alexmingoia/koa-router) [![Tips](https://img.shields.io/gratipay/alexmingoia.svg?style=flat)](https://www.gratipay.com/alexmingoia/) [![Gitter Chat](https://img.shields.io/badge/gitter-join%20chat-1dce73.svg?style=flat)](https://gitter.im/alexmingoia/koa-router/)

> Router middleware for [koa](https://github.com/koajs/koa)

@@ -14,7 +14,16 @@
* Nestable routers.
* ES7 async/await support.

{{#module name="koa-router"}}{{>body}}{{/module}}## Installation
{{#module name="koa-router"}}{{>body}}{{/module}}## Migrating to 7 / Koa 2

- The API has changed to match the new promise-based middleware
signature of koa 2. See the
[koa 2.x readme](https://github.com/koajs/koa/tree/2.0.0-alpha.3) for more
information.
- Middleware is now always run in the order declared by `.use()` (or `.get()`,
etc.), which matches Express 4 API.

## Installation

Install using [npm](https://www.npmjs.org/):

54 changes: 33 additions & 21 deletions lib/layer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var debug = require('debug')('koa-router');
var pathToRegExp = require('path-to-regexp');
var uri = require('urijs');

module.exports = Layer;

@@ -92,6 +93,7 @@ Layer.prototype.params = function (path, captures, existingParams) {
*/

Layer.prototype.captures = function (path) {
if (this.opts.ignoreCaptures) return [];
return path.match(this.regexp).slice(1);
};

@@ -111,27 +113,42 @@ Layer.prototype.captures = function (path) {
* @private
*/

Layer.prototype.url = function (params) {
Layer.prototype.url = function (params, options) {
var args = params;
var url = this.path;
var url = this.path.replace('\(\.\*\)', '');
var toPath = pathToRegExp.compile(url);
var replaced;

// argument is of form { key: val }
if (typeof params != 'object') {
args = Array.prototype.slice.call(arguments);
if (typeof args[args.length - 1] == 'object') {
options = args[args.length - 1];
args = args.slice(0, args.length - 1);
}
}

var tokens = pathToRegExp.parse(url);
var replace = {};

if (args instanceof Array) {
var tokens = pathToRegExp.parse(url);
var replace = {};
for (var len = tokens.length, i=0, j=0; i<len; i++) {
if (tokens[i].name) replace[tokens[i].name] = args[j++];
}
return toPath(replace);
} else if (tokens.some(token => token.name)) {
replace = params;
} else {
options = params;
}
else {
return toPath(params);

replaced = toPath(replace);

if (options && options.query) {
var replaced = new uri(replaced)
replaced.search(options.query);
return replaced.toString();
}

return replaced;
};

/**
@@ -141,13 +158,13 @@ Layer.prototype.url = function (params) {
*
* ```javascript
* router
* .param('user', function *(id, next) {
* this.user = users[id];
* if (!user) return this.status = 404;
* yield next;
* .param('user', function (id, ctx, next) {
* ctx.user = users[id];
* if (!user) return ctx.status = 404;
* next();
* })
* .get('/users/:user', function *(next) {
* this.body = this.user;
* .get('/users/:user', function (ctx, next) {
* ctx.body = ctx.user;
* });
* ```
*
@@ -160,13 +177,8 @@ Layer.prototype.url = function (params) {
Layer.prototype.param = function (param, fn) {
var stack = this.stack;
var params = this.paramNames;
var middleware = function *(next) {
next = fn.call(this, this.params[param], next);
if (typeof next.next === 'function') {
yield *next;
} else {
yield Promise.resolve(next);
}
var middleware = function (ctx, next) {
return fn.call(this, ctx.params[param], ctx, next);
};
middleware.param = param;

351 changes: 187 additions & 164 deletions lib/router.js

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -10,35 +10,37 @@
"lib"
],
"author": "Alex Mingoia <talk@alexmingoia.com>",
"version": "5.4.0",
"version": "7.3.0",
"keywords": [
"koa",
"middleware",
"router",
"route"
],
"dependencies": {
"debug": "^2.2.0",
"debug": "^3.1.0",
"http-errors": "^1.3.1",
"koa-compose": "^3.0.0",
"methods": "^1.0.1",
"path-to-regexp": "^1.1.1"
"path-to-regexp": "^1.1.1",
"urijs": "^1.19.0"
},
"devDependencies": {
"expect.js": "^0.3.1",
"gulp": "^3.8.11",
"gulp-mocha": "^2.0.0",
"jsdoc-to-markdown": "^1.1.1",
"expect.js": "^0.3.1",
"koa": "^0.20.0",
"koa": "2.2.0",
"mocha": "^2.0.1",
"should": "^6.0.3",
"supertest": "^1.0.1"
},
"scripts": {
"test": "[ \"${TRAVIS_NODE_VERSION}\" = \"0.12\" ] && NODE_ENV=test node --harmony-generators node_modules/gulp/bin/gulp.js test || NODE_ENV=test node node_modules/gulp/bin/gulp.js test",
"test": "NODE_ENV=test node node_modules/gulp/bin/gulp.js test",
"docs": "NODE_ENV=test node node_modules/gulp/bin/gulp.js docs"
},
"engines": {
"node": "> 0.12"
"node": ">= 4"
},
"license": "MIT"
}
152 changes: 76 additions & 76 deletions test/lib/layer.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
* Route tests
*/

var koa = require('koa')
var Koa = require('koa')
, http = require('http')
, request = require('supertest')
, Router = require('../../lib/router')
@@ -11,18 +11,18 @@ var koa = require('koa')

describe('Layer', function() {
it('composes multiple callbacks/middlware', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
router.get(
'/:category/:title',
function *(next) {
this.status = 500;
yield next;
function (ctx, next) {
ctx.status = 500;
return next();
},
function *(next) {
this.status = 204;
yield next;
function (ctx, next) {
ctx.status = 204;
return next();
}
);
request(http.createServer(app.callback()))
@@ -36,15 +36,15 @@ describe('Layer', function() {

describe('Layer#match()', function() {
it('captures URL path parameters', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
router.get('/:category/:title', function *(next) {
this.should.have.property('params');
this.params.should.be.type('object');
this.params.should.have.property('category', 'match');
this.params.should.have.property('title', 'this');
this.status = 204;
router.get('/:category/:title', function (ctx) {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', 'match');
ctx.params.should.have.property('title', 'this');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/match/this')
@@ -56,15 +56,15 @@ describe('Layer', function() {
});

it('return orginal path parameters when decodeURIComponent throw error', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
router.get('/:category/:title', function *(next) {
this.should.have.property('params');
this.params.should.be.type('object');
this.params.should.have.property('category', '100%');
this.params.should.have.property('title', '101%');
this.status = 204;
router.get('/:category/:title', function (ctx) {
ctx.should.have.property('params');
ctx.params.should.be.type('object');
ctx.params.should.have.property('category', '100%');
ctx.params.should.have.property('title', '101%');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/100%/101%')
@@ -73,19 +73,19 @@ describe('Layer', function() {
});

it('populates ctx.captures with regexp captures', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
router.get(/^\/api\/([^\/]+)\/?/i, function *(next) {
this.should.have.property('captures');
this.captures.should.be.instanceOf(Array);
this.captures.should.have.property(0, '1');
yield next;
}, function *(next) {
this.should.have.property('captures');
this.captures.should.be.instanceOf(Array);
this.captures.should.have.property(0, '1');
this.status = 204;
router.get(/^\/api\/([^\/]+)\/?/i, function (ctx, next) {
ctx.should.have.property('captures');
ctx.captures.should.be.instanceOf(Array);
ctx.captures.should.have.property(0, '1');
return next();
}, function (ctx) {
ctx.should.have.property('captures');
ctx.captures.should.be.instanceOf(Array);
ctx.captures.should.have.property(0, '1');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/api/1')
@@ -97,19 +97,19 @@ describe('Layer', function() {
});

it('return orginal ctx.captures when decodeURIComponent throw error', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
router.get(/^\/api\/([^\/]+)\/?/i, function *(next) {
this.should.have.property('captures');
this.captures.should.be.type('object');
this.captures.should.have.property(0, '101%');
yield next;
}, function *(next) {
this.should.have.property('captures');
this.captures.should.be.type('object');
this.captures.should.have.property(0, '101%');
this.status = 204;
router.get(/^\/api\/([^\/]+)\/?/i, function (ctx, next) {
ctx.should.have.property('captures');
ctx.captures.should.be.type('object');
ctx.captures.should.have.property(0, '101%');
return next();
}, function (ctx, next) {
ctx.should.have.property('captures');
ctx.captures.should.be.type('object');
ctx.captures.should.have.property(0, '101%');
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/api/101%')
@@ -121,19 +121,19 @@ describe('Layer', function() {
});

it('populates ctx.captures with regexp captures include undefiend', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
router.get(/^\/api(\/.+)?/i, function *(next) {
this.should.have.property('captures');
this.captures.should.be.type('object');
this.captures.should.have.property(0, undefined);
yield next;
}, function *(next) {
this.should.have.property('captures');
this.captures.should.be.type('object');
this.captures.should.have.property(0, undefined);
this.status = 204;
router.get(/^\/api(\/.+)?/i, function (ctx, next) {
ctx.should.have.property('captures');
ctx.captures.should.be.type('object');
ctx.captures.should.have.property(0, undefined);
return next();
}, function (ctx) {
ctx.should.have.property('captures');
ctx.captures.should.be.type('object');
ctx.captures.should.have.property(0, undefined);
ctx.status = 204;
});
request(http.createServer(app.callback()))
.get('/api')
@@ -145,7 +145,7 @@ describe('Layer', function() {
});

it('should throw friendly error message when handle not exists', function() {
var app = koa();
var app = new Koa();
var router = new Router();
app.use(router.routes());
var notexistHandle = undefined;
@@ -165,15 +165,15 @@ describe('Layer', function() {

describe('Layer#param()', function() {
it('composes middleware for param fn', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
var route = new Layer('/users/:user', ['GET'], [function *(next) {
this.body = this.user;
var route = new Layer('/users/:user', ['GET'], [function (ctx) {
ctx.body = ctx.user;
}]);
route.param('user', function *(id, next) {
this.user = { name: 'alex' };
if (!id) return this.status = 404;
yield next;
route.param('user', function (id, ctx, next) {
ctx.user = { name: 'alex' };
if (!id) return ctx.status = 404;
return next();
});
router.stack.push(route);
app.use(router.middleware());
@@ -189,20 +189,20 @@ describe('Layer', function() {
});

it('ignores params which are not matched', function(done) {
var app = koa();
var app = new Koa();
var router = new Router();
var route = new Layer('/users/:user', ['GET'], [function *(next) {
this.body = this.user;
var route = new Layer('/users/:user', ['GET'], [function (ctx) {
ctx.body = ctx.user;
}]);
route.param('user', function *(id, next) {
this.user = { name: 'alex' };
if (!id) return this.status = 404;
yield next;
route.param('user', function (id, ctx, next) {
ctx.user = { name: 'alex' };
if (!id) return ctx.status = 404;
return next();
});
route.param('title', function *(id, next) {
this.user = { name: 'mark' };
if (!id) return this.status = 404;
yield next;
route.param('title', function (id, ctx, next) {
ctx.user = { name: 'mark' };
if (!id) return ctx.status = 404;
return next();
});
router.stack.push(route);
app.use(router.middleware());
@@ -220,15 +220,15 @@ describe('Layer', function() {

describe('Layer#url()', function() {
it('generates route URL', function() {
var route = new Layer('/:category/:title', ['get'], [function* () {}], 'books');
var route = new Layer('/:category/:title', ['get'], [function () {}], 'books');
var url = route.url({ category: 'programming', title: 'how-to-node' });
url.should.equal('/programming/how-to-node');
url = route.url('programming', 'how-to-node');
url.should.equal('/programming/how-to-node');
});

it('escapes using encodeURIComponent()', function() {
var route = new Layer('/:category/:title', ['get'], [function *() {}], 'books');
var route = new Layer('/:category/:title', ['get'], [function () {}], 'books');
var url = route.url({ category: 'programming', title: 'how to node' });
url.should.equal('/programming/how%20to%20node');
});
1,175 changes: 676 additions & 499 deletions test/lib/router.js

Large diffs are not rendered by default.