Skip to content

Commit

Permalink
2.12.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dooy committed Dec 8, 2023
1 parent 81d6afb commit e31bca6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions api/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {
createProxyMiddleware
} = require('http-proxy-middleware')

module.exports = (req, res) => {
let target = ''
let headers= {}
// 代理目标地址
if (req.url.startsWith('/mjapi')) { //这里使用/api可能会与vercel serverless 的 api 路径冲突,根据接口进行调整
target = process.env.MJ_SERVER;
headers= {
'Mj-Api-Secret': process.env.MJ_API_SECRET // 添加自定义请求头
}
}
// 创建代理对象并转发请求
createProxyMiddleware({
target,
changeOrigin: true,
headers,
pathRewrite: {
// 通过路径重写,去除请求路径中的 `/api`
'^/mjapi/': '/'
}
})(req, res)
}
12 changes: 12 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"rewrites": [
{
"source": "/openapi/(.*)",
"destination": "/api/proxy"
},
{
"source": "/mjapi/(.*)",
"destination": "/api/proxy"
}
]
}

0 comments on commit e31bca6

Please sign in to comment.