- Express 式路由使用
app.get
, app.put
, app.post
等
- Named URL parameters and regexp captures
- String or regular expression route matching
- Named Routes with URL generation
- Responds to
OPTIONS
request with allowed methods
- Support for
405 Method Not Allowed
and 501 Not Implemented
- Multiple route middleware
- Multiple routers
安装
npm install koa-router
`</pre>
### 使用
<pre>`var koa = require("koa"),
router = require("koa-router"),
app = koa();
app.use(router(app))
`</pre>
After the router has been initialized you can register routes:
<pre>`app.get("/users/:id", function*(next){
var user = yield User.findOne(this.params.id);
this.body = user;
});
`</pre>
### 多路由
You can use multiple routers and sets of routes by omitting the `app` argument. For example, separate routes for two versions of an API:
<pre>`var koa = require("koa"),
mount = require("mount"),
Router = require("koa-router");
var app = koa();
var APIv1 = new Router();
var APIv2 = new Router();
APIv1.get("/sign-in", function*(){
// ...
})
APIv2.get("/sign-in", function*(){
// ...
})
app.use(mount("/v1", APIv1.middleware())).use(mount("/v2", APIv2.middleware()));
`</pre>
### 链
The http methods(get, post, etc) return their `Router` instance, so routes can be chained as you're used to with express:
<pre>`var api = new Router();
api.get("/foo", showFoo).get("/bar", showBar).post("/foo", createFoo)