• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

koajs/path-match: koa route middleware

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

koajs/path-match

开源软件地址:

https://github.com/koajs/path-match

开源编程语言:

JavaScript 100.0%

开源软件介绍:

Koa Path Match

NPM version Node.js CI Test coverage License Downloads

A simple routing wrapper around path-match. Similar to koa-route, except it optionally handles methods better. All of these routers use path-to-regexp underneath, which is what Express uses as well.

const route = require('koa-path-match')({/* options passed to path-to-regexp */})

app.use(route('/:id(\\d+)', (ctx, next) => {
  const id = ctx.params.id

  // do stuff
  switch (ctx.request.method) {

  }
}))

Or you can create middleware per method:

app.use(route('/:id(\\d+)')
  .get(async ctx => {
    ctx.body = await Things.getById(ctx.params.id)
  })
  .delete(async ctx => {
    await Things.delete(ctx.params.id)
    ctx.status = 204
  })
)

Maintainer

API

route(path, fns...)

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

const router = route(path)

When you don't set fns in the route() function, a router instance is returned.

router[method](fns...)

Define a middleware just for a specific method.

app.use(route('/:id(\\d+)').get(async ctx => {
  ctx.body = await Things.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 to ctx.params, overwriting any already existing keys defined.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap