V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
flyingkid
V2EX  ›  Node.js

koa-router 的一个匹配问题

  •  
  •   flyingkid · Jan 20, 2017 · 2380 views
    This topic created in 3392 days ago, the information mentioned may be changed or developed.
    const router = new KoaRouter({
      prefix: "/articles"
    })
    router.get("/:id/author", articles.author)
    router.get("/:id/info", articles.info)
    router.get("/", articles.index)
    module.exports = router
    

    /1/author 匹配成功,返回内容正常

    /1/author/ 匹配失败,跳到首页

    2 replies    2017-01-25 22:09:13 +08:00
    Arrowing
        1
    Arrowing  
       Jan 20, 2017
    自己做个中间件吧,网上有一个做好的。
    https://www.npmjs.com/package/koa-no-trailing-slash
    magicdawn
        2
    magicdawn  
       Jan 25, 2017   ❤️ 1
    try https://github.com/magicdawn/impress-router

    options.strict 默认 false, trailing slash 可选, 写成 true, 严格匹配

    see path-to-regexp 文档 https://github.com/pillarjs/path-to-regexp#usage
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2670 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 04:00 · PVG 12:00 · LAX 21:00 · JFK 00:00
    ♥ Do have faith in what you're doing.