2016年(48)
分类: JavaScript
2016-10-18 18:35:47
Router middleware for koa. Provides RESTful resource routing.
Router middleware for
See for koa 2.x and async/await support.
Install using :
点击(此处)折叠或打开
Kind: Exported class
Create a new router.
Param | Type | Description |
---|---|---|
[opts] | Object | |
[opts.prefix] | String | prefix router paths |
Example Basic usage:
点击(此处)折叠或打开
Create router.verb() methods, where verb is one of the HTTP verbes such as router.get() or router.post().
Match URL patterns to callback functions or controller actions using router.verb(), where verb is one of the HTTP verbs such as router.get() orrouter.post().
点击(此处)折叠或打开
Route paths will be translated to regular expressions using .
Query strings will not be considered when matching requests.
Routes can optionally have names. This allows generation of URLs and easy renaming of URLs during development.
点击(此处)折叠或打开
Multiple middleware may be given:
点击(此处)折叠或打开
Nesting routers is supported:
点击(此处)折叠或打开
Route paths can be prefixed at the router level:
点击(此处)折叠或打开
Named route parameters are captured and added to ctx.params.
点击(此处)折叠或打开
Kind: instance property of
Param | Type | Description |
---|---|---|
path | String | |
[middleware] | function | route middleware(s) |
callback | function | route callback |
Returns router middleware which dispatches a route matching the request.
Kind: instance property of
Use given middleware(s) before route callback.
Only runs if any route is matched. If a path is given, the middleware will run for any routes that include that path.
Kind: instance method of
Param | Type |
---|---|
[path] | String |
middleware | function |
[...] | function |
Example
点击(此处)折叠或打开
Set the path prefix for a Router instance that was already initialized.
Kind: instance method of
Param | Type |
---|---|
prefix | String |
Example
点击(此处)折叠或打开
Returns separate middleware for responding to OPTIONS requests with an Allow header containing the allowed methods, as well as responding with 405 Method Not Allowed and 501 Not Implemented as appropriate.
Kind: instance method of
Param | Type | Description |
---|---|---|
[options] | Object | |
[options.throw] | Boolean | throw error instead of setting status and header |
[options.notImplemented] | Function | throw the returned value in place of the default NotImplemented error |
[options.methodNotAllowed] | Function | throw the returned value in place of the default MethodNotAllowed error |
Example
Example with
点击(此处)折叠或打开
Redirect source to destination URL with optional 30x status code.
Both source and destination can be route names.
点击(此处)折叠或打开
This is equivalent to:
点击(此处)折叠或打开
Kind: instance method of
Param | Type | Description |
---|---|---|
source | String | URL or route name. |
destination | String | URL or route name. |
code | Number | HTTP status code (default: 301). |
Lookup route with given name.
Kind: instance method of
Param | Type |
---|---|
name | String |
Generate URL for route. Takes the route name and a map of named