router[method](paths, middleware...)

middleware is {Function|Array<Function>|AsyncFunction|Array<AsyncFunction>}

Signature

router

.get('/one', middleware)

.post(['/two','/three'], middleware)

.put(['/four'],[middleware, middleware])

.del('/five', middleware, middleware, middleware)

router.middleware()

Like Express, all routes belong to a single middleware.

You can use koa-mount for mounting of multiple routers:

constKoa=require('koa')

constmount=require('koa-mount')

constRouter=require('koa-trie-router')

let app =newKoa()

let router1 =newRouter()

let router2 =newRouter()

router1.get('/foo', middleware)

router2.get('/bar', middleware)

app.use(mount('/foo',router1.middleware()))

app.use(mount('/bar',router2.middleware()))

router.isImplementedMethod(method)

Checks if the server implements a particular method and returns true or false.
This is not middleware, so you would have to use it in your own middleware.

app.use(function(ctx,next){

if(!router.isImplementedMethod(ctx.method)){

ctx.status=501

return

}

next()

})

ctx.params

ctx.params will be defined with any matched parameters.

router.get('/user/:name',asyncfunction(ctx,next){

let name =ctx.params.name

let user =awaitUser.get(name)

next()

})

Error handling

The middleware throws an error with codeMALFORMEDURL when it encounters
a malformed path. An application can try/catch this upstream, identify the error
by its code, and handle it however the developer chooses in the context of the
application- for example, re-throw as a 404.