Egg: 全局中间件的无法获取params参数

Created on 25 Apr 2018  ·  9Comments  ·  Source: eggjs/egg

全局中间件和插件中间件现在都无法获取URL中的params参数,在实际应用中有时候需求决定了非得在这时获取不可,现在获取不了,十分难搞。能不能解决这个问题?

Most helpful comment

Controller is just a special middleware which exec inside the onion.
And params is provided by Router.

so, if you want to get params at Global Middleware, you could only get it after router matching.

module.exports = () => {
  return async function (ctx, next) {
    await next();
    console.log('after', ctx.params);
  }
}

All 9 comments

Translation of this issue:


global middleware cannot get params parameter

Global middleware and plug-in middleware are unable to obtain the params parameter in the URL. In real applications, sometimes the requirements determine that they must be acquired at this time. Now, it is difficult to obtain. Can you solve this problem?

You can use router middleware

全局中间件和插件中间件都可以访问ctx对象,但这个ctx对象这时的结构相对于后期是不完整的,看上去很奇怪。文档中也没有对此有特别的说明。既然可以访问ctx对象,说明这些中间件是应用级别而不是框架级别的,在其中的业务逻辑,存在需要根据当前请求参数作出相应处理的需求也是合理的。某些全局业务逻辑的预处理会涉及到请求的参数,如果不能在全局中间件和插件中间件中做,则需要每一个路由都单独加一个相同的中间件,是不是比较别扭?

Controller is just a special middleware which exec inside the onion.
And params is provided by Router.

so, if you want to get params at Global Middleware, you could only get it after router matching.

module.exports = () => {
  return async function (ctx, next) {
    await next();
    console.log('after', ctx.params);
  }
}

Controller is just a special middleware which exec inside the onion.
And params is provided by Router.

so, if you want to get params at Global Middleware, you could only get it after router matching.

module.exports = () => {
  return async function (ctx, next) {
    await next();
    console.log('after', ctx.params);
  }
}

完美解决了我的问题~ 点赞

遇到一样的问题。但无法通过上面的方法解决,确实需要在进入 controller 前获取到 params。

例如我有几个这样的 api (都是以 /organizations/:orgId 开头)

/organizations/:orgId/members

现在需要在进入 controller 前,对 orgId 进行鉴权,由于每个 api 都是一样的,所以想要统一在中间件里处理掉。
@atian25

遇到一样的问题。但无法通过上面的方法解决,确实需要在进入 controller 前获取到 params。

看看这个
https://eggjs.org/zh-cn/intro/egg-and-koa.html

方案就是你上面说的这个:

Controller is just a special middleware which exec inside the onion.
And params is provided by Router.
so, if you want to get params at Global Middleware, you could only get it after router matching.

module.exports = () => {
  return async function (ctx, next) {
    await next();
    console.log('after', ctx.params);
  }
}

完美解决了我的问题~ 点赞

遇到一样的问题。但无法通过上面的方法解决,确实需要在进入 controller 前获取到 params。

例如我有几个这样的 api (都是以 /organizations/:orgId 开头)

/organizations/:orgId/members

现在需要在进入 controller 前,对 orgId 进行鉴权,由于每个 api 都是一样的,所以想要统一在中间件里处理掉。
@atian25

你要理解下整个的中间件流程和原理,Router 是最里面的一个 middleware,由它解析后才分派给各个 Controller,你是不可能在洋葱模型的进入阶段去获取到路由参数的(因为 Router 还没被执行)

你这种需求,要不就是加一个路由中间件,要不就是在 Controller 里面处理。

app.router.use('/organizations/:orgId/members', (ctx, next) => { })

or

app.router.get('/organizations/:orgId/members', someAuthMid, controller.xx.xx)

next()后的ctx是完整的

Was this page helpful?
0 / 5 - 0 ratings

Related issues

popomore picture popomore  ·  59Comments

popomore picture popomore  ·  53Comments

XadillaX picture XadillaX  ·  44Comments

killagu picture killagu  ·  48Comments

zrxisme picture zrxisme  ·  44Comments