代码之家  ›  专栏  ›  技术社区  ›  Rahul Sharma

如何在OA中获取中间件的路由路径

  •  0
  • Rahul Sharma  · 技术社区  · 7 年前

    如何获取没有参数的路由的实际路径。

    得到 /users/abc 期望 /users/:id

    路线

    router.get('/user/:id', find);
    

    中间件

    const middleware= (ctx, next) => {
        let path = ctx.path; // '/users/abc'
       // Something 
    };
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   lx1412    7 年前
    const middleware= (ctx, next) => {
        let path = ctx._matchedRoute; // '/users/abc'
       // Something 
    };
    

    源代码

    https://github.com/alexmingoia/koa-router/blob/master/lib/router.js#L336

    https://github.com/alexmingoia/koa-router/blob/master/lib/layer.js#L46

    推荐文章