koa-router
Advanced tools
Comparing version 7.1.1 to 7.2.0
@@ -117,3 +117,3 @@ var debug = require('debug')('koa-router'); | ||
var args = params; | ||
var url = this.path; | ||
var url = this.path.replace('\(\.\*\)', ''); | ||
var toPath = pathToRegExp.compile(url); | ||
@@ -120,0 +120,0 @@ |
@@ -28,7 +28,12 @@ /** | ||
* ```javascript | ||
* var app = require('koa')(); | ||
* var router = require('koa-router')(); | ||
* var Koa = require('koa'); | ||
* var Router = require('koa-router'); | ||
* | ||
* router.get('/', function (ctx, next) {...}); | ||
* var app = new Koa(); | ||
* var router = new Router(); | ||
* | ||
* router.get('/', function (ctx, next) { | ||
* // ctx.router available | ||
* }); | ||
* | ||
* app | ||
@@ -72,2 +77,4 @@ * .use(router.routes()) | ||
* | ||
* Additionaly, `router.all()` can be used to match against all methods. | ||
* | ||
* ```javascript | ||
@@ -86,5 +93,11 @@ * router | ||
* // ... | ||
* }) | ||
* .all('/users/:id', function (ctx, next) { | ||
* // ... | ||
* }); | ||
* ``` | ||
* | ||
* When a route is matched, its path is available at `ctx._matchedRoute` and if named, | ||
* the name is available at `ctx._matchedRouteName` | ||
* | ||
* Route paths will be translated to regular expressions using | ||
@@ -172,3 +185,3 @@ * [path-to-regexp](https://github.com/pillarjs/path-to-regexp). | ||
* | ||
* @name get|put|post|patch|delete | ||
* @name get|put|post|patch|delete|del | ||
* @memberof module:koa-router.prototype | ||
@@ -321,8 +334,14 @@ * @param {String} path | ||
ctx.router = router; | ||
if (!matched.route) return next(); | ||
var mostSpecificPath = matched.pathAndMethod[matched.pathAndMethod.length - 1].path; | ||
ctx._matchedRoute = mostSpecificPath; | ||
var matchedLayers = matched.pathAndMethod | ||
var mostSpecificLayer = matchedLayers[matchedLayers.length - 1] | ||
ctx._matchedRoute = mostSpecificLayer.path; | ||
if (mostSpecificLayer.name) { | ||
ctx._matchedRouteName = mostSpecificLayer.name; | ||
} | ||
layerChain = matched.pathAndMethod.reduce(function(memo, layer) { | ||
layerChain = matchedLayers.reduce(function(memo, layer) { | ||
memo.push(function(ctx, next) { | ||
@@ -352,5 +371,8 @@ ctx.captures = layer.captures(path, ctx.captures); | ||
* ```javascript | ||
* var app = koa(); | ||
* var router = router(); | ||
* var Koa = require('koa'); | ||
* var Router = require('koa-router'); | ||
* | ||
* var app = new Koa(); | ||
* var router = new Router(); | ||
* | ||
* app.use(router.routes()); | ||
@@ -360,2 +382,20 @@ * app.use(router.allowedMethods()); | ||
* | ||
* **Example with [Boom](https://github.com/hapijs/boom)** | ||
* | ||
* ```javascript | ||
* var Koa = require('koa'); | ||
* var Router = require('koa-router'); | ||
* var Boom = require('boom'); | ||
* | ||
* var app = new Koa(); | ||
* var router = new Router(); | ||
* | ||
* app.use(router.routes()); | ||
* app.use(router.allowedMethods({ | ||
* throw: true, | ||
* notImplemented: () => new Boom.notImplemented(), | ||
* methodNotAllowed: () => new Boom.methodNotAllowed() | ||
* })); | ||
* ``` | ||
* | ||
* @param {Object=} options | ||
@@ -563,5 +603,7 @@ * @param {Boolean=} options.throw throw error instead of setting status and header | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* router.get('user', '/users/:id', function (ctx, next) { | ||
* // ... | ||
* // ... | ||
* }); | ||
@@ -574,2 +616,7 @@ * | ||
* // => "/users/3" | ||
* | ||
* router.use(function (ctx, next) { | ||
* // redirect to named route | ||
* ctx.redirect(ctx.router.url('sign-in')); | ||
* }) | ||
* ``` | ||
@@ -576,0 +623,0 @@ * |
@@ -13,3 +13,3 @@ { | ||
"author": "Alex Mingoia <talk@alexmingoia.com>", | ||
"version": "7.1.1", | ||
"version": "7.2.0", | ||
"keywords": [ | ||
@@ -16,0 +16,0 @@ "koa", |
149
README.md
# koa-router | ||
[![NPM version](https://img.shields.io/badge/npm-v7.0.1-blue.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](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/) | ||
@@ -15,3 +15,3 @@ > Router middleware for [koa](https://github.com/koajs/koa) | ||
* Nestable routers. | ||
* ES7 async/await support (koa-router 7.x). | ||
* ES7 async/await support. | ||
@@ -29,33 +29,32 @@ ## Migrating to 7 / Koa 2 | ||
Until koa 2 is released, you must specify `^7.0.0` or use the `next` tag: | ||
Install using [npm](https://www.npmjs.org/): | ||
```sh | ||
npm install koa-router@next | ||
npm install koa-router | ||
``` | ||
When koa 2 is released, 7.x will be tagged with `latest` and installed by | ||
default by NPM. | ||
## API Reference | ||
* [koa-router](#module_koa-router) | ||
* [Router](#exp_module_koa-router--Router) ⏏ | ||
* [new Router([opts])](#new_module_koa-router--Router_new) | ||
* _instance_ | ||
* [.get|put|post|patch|delete](#module_koa-router--Router+get|put|post|patch|delete) ⇒ <code>Router</code> | ||
* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code> | ||
* [.use([path], middleware, [...])](#module_koa-router--Router+use) ⇒ <code>Router</code> | ||
* [.prefix(prefix)](#module_koa-router--Router+prefix) ⇒ <code>Router</code> | ||
* [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ <code>function</code> | ||
* [.redirect(source, destination, code)](#module_koa-router--Router+redirect) ⇒ <code>Router</code> | ||
* [.route(name)](#module_koa-router--Router+route) ⇒ <code>Layer</code> | <code>false</code> | ||
* [.url(name, params)](#module_koa-router--Router+url) ⇒ <code>String</code> | <code>Error</code> | ||
* [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code> | ||
* _static_ | ||
* [.url(path, params)](#module_koa-router--Router.url) ⇒ <code>String</code> | ||
* [Router](#exp_module_koa-router--Router) ⏏ | ||
* [new Router([opts])](#new_module_koa-router--Router_new) | ||
* _instance_ | ||
* [.get|put|post|patch|delete|del](#module_koa-router--Router+get|put|post|patch|delete|del) ⇒ <code>Router</code> | ||
* [.routes](#module_koa-router--Router+routes) ⇒ <code>function</code> | ||
* [.use([path], middleware)](#module_koa-router--Router+use) ⇒ <code>Router</code> | ||
* [.prefix(prefix)](#module_koa-router--Router+prefix) ⇒ <code>Router</code> | ||
* [.allowedMethods([options])](#module_koa-router--Router+allowedMethods) ⇒ <code>function</code> | ||
* [.redirect(source, destination, code)](#module_koa-router--Router+redirect) ⇒ <code>Router</code> | ||
* [.route(name)](#module_koa-router--Router+route) ⇒ <code>Layer</code> | <code>false</code> | ||
* [.url(name, params)](#module_koa-router--Router+url) ⇒ <code>String</code> | <code>Error</code> | ||
* [.param(param, middleware)](#module_koa-router--Router+param) ⇒ <code>Router</code> | ||
* _static_ | ||
* [.url(path, params)](#module_koa-router--Router.url) ⇒ <code>String</code> | ||
<a name="exp_module_koa-router--Router"></a> | ||
### Router ⏏ | ||
**Kind**: Exported class | ||
**Kind**: Exported class | ||
<a name="new_module_koa-router--Router_new"></a> | ||
#### new Router([opts]) | ||
@@ -70,3 +69,3 @@ Create a new router. | ||
**Example** | ||
**Example** | ||
Basic usage: | ||
@@ -81,3 +80,5 @@ | ||
router.get('/', function (ctx, next) {...}); | ||
router.get('/', function (ctx, next) { | ||
// ctx.router available | ||
}); | ||
@@ -88,4 +89,5 @@ app | ||
``` | ||
<a name="module_koa-router--Router+get|put|post|patch|delete"></a> | ||
#### router.get|put|post|patch|delete ⇒ <code>Router</code> | ||
<a name="module_koa-router--Router+get|put|post|patch|delete|del"></a> | ||
#### router.get|put|post|patch|delete|del ⇒ <code>Router</code> | ||
Create `router.verb()` methods, where *verb* is one of the HTTP verbs such | ||
@@ -118,2 +120,5 @@ as `router.get()` or `router.post()`. | ||
When a route is matched, its path is available at `ctx._matchedRoute` and if named, | ||
the name is available at `ctx._matchedRouteName` | ||
Route paths will be translated to regular expressions using | ||
@@ -148,3 +153,3 @@ [path-to-regexp](https://github.com/pillarjs/path-to-regexp). | ||
ctx.user = user; | ||
return next(); | ||
next(); | ||
}); | ||
@@ -199,6 +204,6 @@ }, | ||
The [path-to-regexp](https://github.com/pillarjs/path-to-regexp) module is used | ||
to convert paths to regular expressions. | ||
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> | ||
**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code> | ||
@@ -212,8 +217,10 @@ | Param | Type | Description | | ||
<a name="module_koa-router--Router+routes"></a> | ||
#### router.routes ⇒ <code>function</code> | ||
Returns router middleware which dispatches a route matching the request. | ||
**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance property of <code>[Router](#exp_module_koa-router--Router)</code> | ||
<a name="module_koa-router--Router+use"></a> | ||
#### router.use([path], middleware, [...]) ⇒ <code>Router</code> | ||
#### router.use([path], middleware) ⇒ <code>Router</code> | ||
Use given middleware. | ||
@@ -225,11 +232,11 @@ | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
| Param | Type | | ||
| --- | --- | | ||
| [path] | <code>String</code> | | ||
| middleware | <code>function</code> | | ||
| [...] | <code>function</code> | | ||
| [path] | <code>String</code> | | ||
| middleware | <code>function</code> | | ||
| [...] | <code>function</code> | | ||
**Example** | ||
**Example** | ||
```javascript | ||
@@ -250,12 +257,13 @@ // session middleware will run before authorize | ||
<a name="module_koa-router--Router+prefix"></a> | ||
#### router.prefix(prefix) ⇒ <code>Router</code> | ||
Set the path prefix for a Router instance that was already initialized. | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
| Param | Type | | ||
| --- | --- | | ||
| prefix | <code>String</code> | | ||
| prefix | <code>String</code> | | ||
**Example** | ||
**Example** | ||
```javascript | ||
@@ -265,2 +273,3 @@ router.prefix('/things/:thing_id') | ||
<a name="module_koa-router--Router+allowedMethods"></a> | ||
#### router.allowedMethods([options]) ⇒ <code>function</code> | ||
@@ -271,3 +280,3 @@ Returns separate middleware for responding to `OPTIONS` requests with | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
@@ -278,6 +287,6 @@ | Param | Type | Description | | ||
| [options.throw] | <code>Boolean</code> | throw error instead of setting status and header | | ||
| [options.notImplemented] | <code>Function</code> | throw the returned value in place of the default NotImplemented error | | ||
| [options.methodNotAllowed] | <code>Function</code> | throw the returned value in place of the default MethodNotAllowed error | | ||
| [options.notImplemented] | <code>function</code> | throw the returned value in place of the default NotImplemented error | | ||
| [options.methodNotAllowed] | <code>function</code> | throw the returned value in place of the default MethodNotAllowed error | | ||
**Example** | ||
**Example** | ||
```javascript | ||
@@ -293,3 +302,5 @@ var Koa = require('koa'); | ||
``` | ||
**Example with [Boom](https://github.com/hapijs/boom)** | ||
```javascript | ||
@@ -311,2 +322,3 @@ var Koa = require('koa'); | ||
<a name="module_koa-router--Router+redirect"></a> | ||
#### router.redirect(source, destination, code) ⇒ <code>Router</code> | ||
@@ -330,3 +342,3 @@ Redirect `source` to `destination` URL with optional 30x status `code`. | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
@@ -340,18 +352,28 @@ | Param | Type | Description | | ||
<a name="module_koa-router--Router+route"></a> | ||
#### router.route(name) ⇒ <code>Layer</code> | <code>false</code> | ||
Lookup route with given `name`. | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
| Param | Type | | ||
| --- | --- | | ||
| name | <code>String</code> | | ||
| name | <code>String</code> | | ||
<a name="module_koa-router--Router+url"></a> | ||
#### router.url(name, params) ⇒ <code>String</code> | <code>Error</code> | ||
Generate URL for route. Takes the route name and a map of named `params`. | ||
Generate URL for route. Takes a route name and map of named `params`. | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>String</code> | route name | | ||
| params | <code>Object</code> | url parameters | | ||
**Example** | ||
```javascript | ||
router.get('user', '/users/:id', function (ctx, next) { | ||
// ... | ||
// ... | ||
}); | ||
@@ -364,12 +386,10 @@ | ||
// => "/users/3" | ||
router.use(function (ctx, next) { | ||
// redirect to named route | ||
ctx.redirect(ctx.router.url('sign-in')); | ||
}) | ||
``` | ||
<a name="module_koa-router--Router+param"></a> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>String</code> | route name | | ||
| params | <code>Object</code> | url parameters | | ||
<a name="module_koa-router--Router+param"></a> | ||
#### router.param(param, middleware) ⇒ <code>Router</code> | ||
@@ -379,10 +399,10 @@ Run middleware for named route parameters. Useful for auto-loading or | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: instance method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
| Param | Type | | ||
| --- | --- | | ||
| param | <code>String</code> | | ||
| middleware | <code>function</code> | | ||
| param | <code>String</code> | | ||
| middleware | <code>function</code> | | ||
**Example** | ||
**Example** | ||
```javascript | ||
@@ -407,6 +427,7 @@ router | ||
<a name="module_koa-router--Router.url"></a> | ||
#### Router.url(path, params) ⇒ <code>String</code> | ||
Generate URL from url pattern and given `params`. | ||
**Kind**: static method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
**Kind**: static method of <code>[Router](#exp_module_koa-router--Router)</code> | ||
@@ -418,3 +439,3 @@ | Param | Type | Description | | ||
**Example** | ||
**Example** | ||
```javascript | ||
@@ -421,0 +442,0 @@ var url = Router.url('/users/:id', {id: 1}); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
43972
835
434
2