koa-router
Advanced tools
Comparing version 6.2.0 to 7.0.0
# History | ||
## 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. | ||
- Register multiple routes with array of paths [#203]. | ||
## 6.2.0 | ||
@@ -4,0 +14,0 @@ |
@@ -165,2 +165,5 @@ /** | ||
* | ||
* The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is | ||
* used to convert paths to regular expressions. | ||
* | ||
* @name get|put|post|patch|delete | ||
@@ -198,6 +201,7 @@ * @memberof module:koa-router.prototype | ||
/** | ||
* Use given middleware(s) before route callback. | ||
* Use given middleware. | ||
* | ||
* Only runs if any route is matched. If a path is given, the middleware will | ||
* run for any routes that include that path. | ||
* Middleware run in the order they are defined by `.use()`. They are invoked | ||
* sequentially, requests start at the first middleware and work their way | ||
* "down" the middleware stack. | ||
* | ||
@@ -207,3 +211,6 @@ * @example | ||
* ```javascript | ||
* router.use(session(), authorize()); | ||
* // session middleware will run before authorize | ||
* router | ||
* .use(session()) | ||
* .use(authorize()); | ||
* | ||
@@ -214,3 +221,3 @@ * // use middleware only with given path | ||
* // or with an array of paths | ||
* // router.use(['/users', '/admin'], userAuth()); | ||
* router.use(['/users', '/admin'], userAuth()); | ||
* | ||
@@ -229,3 +236,3 @@ * app.use(router.routes()); | ||
var middleware = Array.prototype.slice.call(arguments); | ||
var path; | ||
var path = '(.*)'; | ||
@@ -245,9 +252,8 @@ // support array of paths | ||
// filter out nested routers from filter | ||
middleware = middleware.filter(function (fn) { | ||
if (fn.router) { | ||
fn.router.stack.forEach(function (layer) { | ||
if (path) layer.setPrefix(path); | ||
if (router.opts.prefix) layer.setPrefix(router.opts.prefix); | ||
router.stack.push(layer); | ||
middleware.forEach(function (m) { | ||
if (m.router) { | ||
m.router.stack.forEach(function (nestedLayer) { | ||
if (path) nestedLayer.setPrefix(path); | ||
if (router.opts.prefix) nestedLayer.setPrefix(router.opts.prefix); | ||
router.stack.push(nestedLayer); | ||
}); | ||
@@ -257,18 +263,10 @@ | ||
Object.keys(router.params).forEach(function (key) { | ||
fn.router.param(key, router.params[key]); | ||
m.router.param(key, router.params[key]); | ||
}); | ||
} | ||
return false; | ||
} else { | ||
router.register(path, [], m, { end: false }); | ||
} | ||
return true; | ||
}); | ||
if (middleware.length) { | ||
router.register(path || '(.*)', [], middleware, { | ||
end: false | ||
}); | ||
} | ||
return this; | ||
@@ -502,4 +500,14 @@ }; | ||
var router = this; | ||
var stack = this.stack; | ||
// support array of paths | ||
if (Array.isArray(path)) { | ||
path.forEach(function (p) { | ||
router.register.call(router, p, methods, middleware, opts); | ||
}); | ||
return this; | ||
} | ||
// create route | ||
@@ -523,35 +531,4 @@ var route = new Layer(path, methods, middleware, { | ||
// register route with router | ||
if (methods.length || !stack.length) { | ||
// if we don't have parameters, put before any with same route | ||
// nesting level but with parameters | ||
var added = false; | ||
stack.push(route); | ||
if (!route.paramNames.length) { | ||
var routeNestingLevel = route.path.toString().split('/').length; | ||
added = stack.some(function (m, i) { | ||
var mNestingLevel = m.path.toString().split('/').length; | ||
var isParamRoute = !!m.paramNames.length; | ||
if (routeNestingLevel === mNestingLevel && isParamRoute) { | ||
return stack.splice(i, 0, route); | ||
} | ||
}); | ||
} | ||
if (!added) stack.push(route); | ||
} else { | ||
stack.some(function (m, i) { | ||
if (!m.methods.length && i === stack.length - 1) { | ||
return stack.push(route); | ||
} else if (m.methods.length) { | ||
if (stack[i - 1]) { | ||
return stack.splice(i, 0, route); | ||
} else { | ||
return stack.unshift(route); | ||
} | ||
} | ||
}); | ||
} | ||
return route; | ||
@@ -558,0 +535,0 @@ }; |
@@ -13,3 +13,3 @@ { | ||
"author": "Alex Mingoia <talk@alexmingoia.com>", | ||
"version": "6.2.0", | ||
"version": "7.0.0", | ||
"keywords": [ | ||
@@ -16,0 +16,0 @@ "koa", |
@@ -17,7 +17,10 @@ # koa-router | ||
## Koa 2.x | ||
## Migrating to 7 / Koa 2 | ||
koa-router 6.x requires | ||
[koa 2.x](https://github.com/koajs/koa/tree/2.0.0-alpha.3). For koa 1.x use | ||
koa-router 5.x. | ||
- 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. | ||
@@ -178,2 +181,5 @@ ## Installation | ||
The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is used | ||
to convert paths to regular expressions. | ||
**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code> | ||
@@ -194,6 +200,7 @@ | ||
#### router.use([path], middleware, [...]) ⇒ <code>Router</code> | ||
Use given middleware(s) before route callback. | ||
Use given middleware. | ||
Only runs if any route is matched. If a path is given, the middleware will | ||
run for any routes that include that path. | ||
Middleware run in the order they are defined by `.use()`. They are invoked | ||
sequentially, requests start at the first middleware and work their way | ||
"down" the middleware stack. | ||
@@ -210,3 +217,6 @@ **Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
```javascript | ||
router.use(session(), authorize()); | ||
// session middleware will run before authorize | ||
router | ||
.use(session()) | ||
.use(authorize()); | ||
@@ -217,3 +227,3 @@ // use middleware only with given path | ||
// or with an array of paths | ||
// router.use(['/users', '/admin'], userAuth()); | ||
router.use(['/users', '/admin'], userAuth()); | ||
@@ -220,0 +230,0 @@ app.use(router.routes()); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41516
397
789