tuax / tua-api

🏗 一款可配置的通用 api 请求函数生成工具(A common tool helps converting configs to api functions)

Home Page:https://tuax.github.io/tua-api/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat(middleware): 通过中间件修改 host

BuptStEve opened this issue · comments

你的功能请求是否与某些问题相关?请描述
在 Node 端时希望根据 url query 中的字段,动态改变 host

描述您想要的解决方案
添加一个中间件修改 ctx.req.host 即可

const hostMiddleware = async (ctx, next) => {
  ctx.req.host = 'new host'
  await next()
}

但是目前使用 host 的中间件运行在自定义中间件之前,因此以上写法无法生效。
所以打算拆分 formatReqParamsMiddleware 中间件,将使用 host 的代码移到 setFullUrlMiddleware 中间件中。

附加上下文
https://github.com/tuateam/tua-api/blob/131c98f7555c9caea640548c38a33a339733b5e9/src/middlewareFns.js#L78