1 Star 20 Fork 4

mayuxian/vue3-admin-template

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
proxy-server.js 1.40 KB
Copy Edit Raw Blame History
mayuxian authored 3 years ago . 初始化项目
//TODO:可本地mock数据,暂未调试
const httpProxy = require('http-proxy')
const proxy = httpProxy.createProxyServer()
const mockServer = require('./mock-server')
function proxyServer(req, res, next) {
// console.log('--------proxy req------------', req)
//若为XMLHttpRequest,则为ajax请求
if (
['XMLHttpRequest', 'fetch'].includes(req.headers['x-requested-with']) ||
//识别出文件上传请求,进行代理转发
(req.headers['content-type'] &&
req.headers['content-type']
.toLowerCase()
.includes('multipart/form-data;'))
) {
if (process.env.ENV === 'mock') {
console.log('mock -> ' + req.url)
return mockServer.call(this, req, res, next)
}
const proxyUrl = process.env.VUE_APP_BASE_API
console.log('proxy -> ' + proxyUrl + req.url)
const cookieDomainRewrite = {}
cookieDomainRewrite[req.host] = proxyUrl
return proxy.web(
req,
res,
{
target: proxyUrl,
changeOrigin: true,
// autoRewrite: true,
headers: {
host: req.host,
},
// hostRewrite: req.host,
// cookieDomainRewrite: cookieDomainRewrite
// cookieDomainRewrite: {
// "old.domain": "new.domain",
// }
},
err => {
console.error(err)
}
)
}
return next()
}
module.exports = (app, server, compiler) => {
app.use(proxyServer)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mayuxian/vue3-admin-template.git
git@gitee.com:mayuxian/vue3-admin-template.git
mayuxian
vue3-admin-template
vue3-admin-template
main

Search