egg 的路由带参问题

Created on 12 Sep 2018  ·  12Comments  ·  Source: eggjs/egg

// 200
router.get('/a/:id', controller...) // http://domian.com/a/1

// 404 
router.get('/a/b/:id', controller...) // http://domian.com/a/b/1

---

// 第二个路由错在哪里,请问怎么才能在二级目录下带参
invalid

All 12 comments

Translation of this issue:


egg's routing with parameters

// 200
Router.get('/a/:id', controller...) // http://domian.com/a/1

// 404
Router.get('/a/b/:id', controller...) // http://domian.com/a/b/1

---

// Where is the second route wrong? How can I take the parameter in the secondary directory?
// 200
router.get('/a/:id/b/:id', controller...) // http://domian.com/a/1/b/1


// 404
router.get('/a/:id/b', controller...) // http://domian.com/a/1/b


---

// 所以我搞不明白,如果可以,我觉得我们还是先解决第一个问题,它影响了我的工作。

因为你前一个通配吃掉了后一个的啊,自己调整顺序

因为你前一个通配吃掉了后一个的啊,自己调整顺序

// 404 
router.get('/a/b/:id', controller...) // http://domian.com/a/b/1

// 请问我要怎么调整,才能匹配到  http://domian.com/a/b/*
我们的能不能搞成 /: 就代表 *

你可以自己写正则式。

https://github.com/alexmingoia/koa-routerhttps://github.com/pillarjs/path-to-regexp 文档去。

文档里支持这种格式的,但事实是不可以的

4C`9N%CG__$S})0W7RLNRMO.png

谁说不支持啊。。。 你的问题在于你前一个路由干掉了后一个路由,自己调整 2 行代码的顺序

谁说不支持啊。。。 你的问题在于你前一个路由干掉了后一个路由,自己调整 2 行代码的顺序

代码只是演示啊,我每次都定义一个路由,没有冲突问题

router.get('/a/:id/b', controller...)

router.get('/c/d/:id', controller...) 

都不可以的啊,你自己试下嘛

我找到问题了,是我自己的问题

@jasontsai1990 :没有关系,你也可以详细告知问题所在,没准还有和你相同问题的人。

@jasontsai1990 有问题可以随时问,没事的,不过有几点建议:

  • 尽可能的减少沟通成本,避免 『如何向开源项目提交无法解答的问题』
  • 绝大部分情况下,通过 egg-init --type=simple 来初始化个最小可复现仓库,这个过程中你就会自己发现问题了,这是一种非常高效的问题定位方式。
  • 如果还不能解决,此时直接提交到你的 GitHub ,我们来跟进,就会非常的高效,避免挤药膏似的交流。
  • 问题解决后,及时关闭 issue,并把解决方式同步进来,方便后来人。issue 本身就是一种非常好的可沉淀的交流方式。
Was this page helpful?
0 / 5 - 0 ratings

Related issues

popomore picture popomore  ·  47Comments

killagu picture killagu  ·  48Comments

XadillaX picture XadillaX  ·  44Comments

popomore picture popomore  ·  70Comments

fengmk2 picture fengmk2  ·  44Comments