Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x 路由混乱 #1901

Closed
drunken005 opened this issue Dec 29, 2017 · 8 comments
Closed

2.x 路由混乱 #1901

drunken005 opened this issue Dec 29, 2017 · 8 comments

Comments

@drunken005
Copy link

定义接口路由如下:
GET - /api/users/:id
GET - /api/users/address
GET - /api/users/wallet
问题:请求 /api/users/wallet 或 /api/users/address 时路由识别的时 /api/users/:id,执行的是/api/users/:id对应的controller, 在 1.x中不存在这个问题,升级2.x后出现

@SunShinewyf
Copy link
Contributor

试了一下,没有复现出来,求复现代码~

@zhengjunxin
Copy link

是不是把 /api/users/:id 的路由写在前面了?

@fsiaonma
Copy link
Contributor

fsiaonma commented Dec 29, 2017

router.js

module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.get('/api/users/:id', controller.home.a);
  router.get('/api/users/address', controller.home.b);
  router.get('/api/users/wallet', controller.home.c);
};

controller/home.js

'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    this.ctx.body = 'hi, egg';
  }
  
  async a() {
    console.log(1);
  }

  async b() {
    console.log(2);
  }

  async c() {
    console.log(3);
  }
}

module.exports = HomeController;

访问 http://localhost:7001/api/users/wallet

输出:

# 1

@drunken005 是这个意思吗?

@popomore
Copy link
Member

最好不要写这种有歧义的路由,你可以在 controller 里面判断 id 在调用对应的方法

@fsiaonma
Copy link
Contributor

fsiaonma commented Dec 29, 2017

这是 koa-router 的原因,egg2.0 router 继承 koa-router 0.7.3

测试了一下 koa-router

const Koa = require('koa');
const app = new Koa();

const Router = require('koa-router');
var router = new Router();

router.get('/api/users/:id', (ctx, next) => {
  console.log(1);
});

router.get('/api/users/wallet', (ctx, next) => {
  console.log(2);
});

app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);

请求 /api/users/wallet 输出:

# 1

调换一下位置:

const Koa = require('koa');
const app = new Koa();

const Router = require('koa-router');
var router = new Router();

router.get('/api/users/wallet', (ctx, next) => {
  console.log(2);
});

router.get('/api/users/:id', (ctx, next) => {
  console.log(1);
});

app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);

请求 /api/users/wallet 输出:

# 2

由此可见 koa-router 0.7.3 匹配规则是,无论是否有 Path-Params,以匹配到的第一个为准。

至于要找到根源,需要 egg 1.x 的版本 和 koa-router 的版本对应关系,才好定位真正的变化位置。

当然也建议尽量不要定义这样有歧义的路由。

@drunken005 FYI.

@atian25
Copy link
Member

atian25 commented Jan 2, 2018

感觉是一个典型的 bug 当做 feature 的案例。

@atian25 atian25 closed this as completed Jan 2, 2018
@zoulux
Copy link

zoulux commented Jan 3, 2018

@atian25 请问,像当前用户地址的restful的url怎样写比较好

@atian25
Copy link
Member

atian25 commented Jan 3, 2018

就他这个案例,通配的放到后面就好了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants