Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

koa route middleware

License

NotificationsYou must be signed in to change notification settings

koajs/path-match

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM versionNode.js CITest coverageLicenseDownloads

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}))

Maintainer

API

route(path, fns...)

paths are just like Express routes.fns is either a single middlewareor nested arrays of middleware, just like Express.

const router = route(path)

When you don't setfns in theroute() function, a router instance is returned.

router[method](fns...)

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.

this.params

Any keys defined in the path will be set toctx.params,overwriting any already existing keys defined.


[8]ページ先頭

©2009-2025 Movatter.jp