-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Comments
试了一下,没有复现出来,求复现代码~ |
是不是把 /api/users/:id 的路由写在前面了? |
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; 访问 输出: # 1 @drunken005 是这个意思吗? |
最好不要写这种有歧义的路由,你可以在 controller 里面判断 id 在调用对应的方法 |
这是 测试了一下 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); 请求
调换一下位置: 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); 请求
由此可见 至于要找到根源,需要 当然也建议尽量不要定义这样有歧义的路由。 @drunken005 FYI. |
感觉是一个典型的 bug 当做 feature 的案例。 |
@atian25 请问,像当前用户地址的restful的url怎样写比较好 |
就他这个案例,通配的放到后面就好了。 |
定义接口路由如下:
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后出现
The text was updated successfully, but these errors were encountered: