- Notifications
You must be signed in to change notification settings - Fork5
koajs/path-match
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A simple routing wrapper aroundpath-match.Similar tokoa-route, except it optionally handles methods better.All of these routers usepath-to-regexpunderneath, which is what Express uses as well.
constroute=require('koa-path-match')({/* options passed to path-to-regexp */})app.use(route('/:id',(ctx,next)=>{constid=ctx.params.id// do stuffswitch(ctx.request.method){}}))
Or you can create middleware per method:
app.use(route('/:id').get(asyncctx=>{ctx.body=awaitThings.getById(ctx.params.id)}).delete(asyncctx=>{awaitThings.delete(ctx.params.id)ctx.status=204}))
- Lead: @jonathanong@jongleberry
- Team: @koajs/routing
path
s are just like Express routes.fns
is either a single middlewareor nested arrays of middleware, just like Express.
When you don't setfns
in theroute()
function, a router instance is returned.
Define a middleware just for a specific method.
app.use(route('/:id').get(asyncctx=>{ctx.body=awaitThings.getById(ctx.params.id)}))
next
is not passed as a parameter.I consider this an anti-pattern in Koa - one route/method, one function.
Any keys defined in the path will be set toctx.params
,overwriting any already existing keys defined.
About
koa route middleware
Resources
License
Code of conduct
Security policy
Stars
Watchers
Forks
Packages0
No packages published