koa-rutt
Swedish routing, for Koa.
Install
npm install koa-rutt
Usage
Most features will be demonstrated below.
var app = ; Router = ; var router = ; // Add pre-middleware.router; // You can also return individual Route objects if you preferrouter; // Add the router as middleware to the app.app; app;
API
Route
class new Route (router, path)
The route.
Name | Type | Attributes | Description |
---|---|---|---|
router | Router |
The Router this Route is attached to. | |
path | String |
The URL path. |
Route#get|post|delete|put (...middleware) → {Route}
Assign middleware to be run upon relevant HTTP method being triggered.
Name | Type | Attributes | Description |
---|---|---|---|
middleware | GeneratorFunction |
multiple | Middleware to be attached to called HTTP method. |
Returns: Route
Returns this instance of Route.
Router
class new Router ([options])
The router.
Name | Type | Attributes | Description |
---|---|---|---|
[options] | Object |
optional | Options object |
options.prefix | string |
The prefix of each route of this router. |
Router#get|post|delete|put (path, ...middleware) → {Router}
Assign middleware to be run upon relevant HTTP method being triggered.
Name | Type | Attributes | Description |
---|---|---|---|
path | String |
The path to the relevant Route. | |
middleware | GeneratorFunction |
multiple | Middleware to be attached to called HTTP method. |
Returns: Router
Returns this instance of Router.
Router#route (path, [methods]) → {Router|Route}
Create or get a Route from the Router object, or HTTP methods on Route by using the optional methods parameter.
Name | Type | Attributes | Description |
---|---|---|---|
path | String |
The URL path to the resource. | |
[methods] | Object |
optional | An object with HTTP methods. |
Returns: Router|Route
Returns this instance of Router, or Route for path if no methods specified.
Example: Usage with optional methods parameter
router
Example: Usage with only path parameter
const testRoute = router
Router#pre ([method], ...middleware) → {Router}
Define middleware to run prior to HTTP method middleware. If no method provided, the middleware will run before all other middlewares on the router.
Name | Type | Attributes | Description |
---|---|---|---|
[method] | string |
optional | The HTTP method (eg 'get') to add pre-middleware to. |
middleware | GeneratorFunction |
multiple | The middleware to attach. |
Returns: Router
Returns this instance of Router.
Example: Example of #pre usage.
router
Router#middleware () → {GeneratorFunction}
Returns the middleware to be provided to the Koa app instance.
Returns: GeneratorFunction
Middleware to provide to Koa.
License
BSD 2-clause license. See LICENSE.