koa-tree-router
Advanced tools
Comparing version
{ | ||
"name": "koa-tree-router", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "A high performance koa router", | ||
@@ -16,2 +16,7 @@ "main": "router.js", | ||
}, | ||
"files": [ | ||
"*.js", | ||
"*.md", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
@@ -31,2 +36,3 @@ "koa", | ||
"homepage": "https://github.com/steambap/koa-tree-router#readme", | ||
"typings": "index.d.ts", | ||
"devDependencies": { | ||
@@ -33,0 +39,0 @@ "beautify-benchmark": "^0.2.4", |
# Koa tree router | ||
Koa tree router a high performance router for Koa. | ||
[](https://travis-ci.org/steambap/koa-tree-router) | ||
Koa tree router is a high performance router for Koa. | ||
## Features | ||
@@ -11,2 +13,4 @@ | ||
- Support for 405 method not allowed | ||
- Multiple middlewares per route | ||
@@ -35,2 +39,61 @@ | ||
app.listen(8080); | ||
``` | ||
## API | ||
#### Router([options]) | ||
Instance a new router. | ||
You can pass a middleware with the option `onMethodNotAllowed`. | ||
```js | ||
const router = require('koa-tree-router')({ | ||
onMethodNotAllowed(ctx){ | ||
ctx.body = "not allowed" | ||
} | ||
}) | ||
``` | ||
#### on(method, path, middleware) | ||
Register a new route. | ||
```js | ||
router.on('GET', '/example', (ctx) => { | ||
// your code | ||
}) | ||
``` | ||
#### Shorthand methods | ||
If you want tp get expressive, here is what you can do: | ||
```js | ||
router.get(path, middleware) | ||
router.delete(path, middleware) | ||
router.head(path, middleware) | ||
router.patch(path, middleware) | ||
router.post(path, middleware) | ||
router.put(path, middleware) | ||
router.options(path, middleware) | ||
router.trace(path, middleware) | ||
router.connect(path, middleware) | ||
``` | ||
If you need a route that supports *all* methods you can use the `all` api. | ||
```js | ||
router.all(path, middleware) | ||
``` | ||
#### routes | ||
Returns router middleware. | ||
```JS | ||
app.use(router.routes()); | ||
``` | ||
#### ctx.params | ||
This object contains key-value pairs of named route parameters. | ||
```JS | ||
router.get("/user/:name", function() { | ||
// your code | ||
}); | ||
// GET /user/1 | ||
ctx.params.name | ||
// => "1" | ||
``` |
97
185.29%20208
-62.57%6
-40%537
-32.03%