我使用的是o2-cmpt创建的react应用,host是域名的话会跨域,而host是IP地址的话不会,你们在node_modules里的@o2oa包里已经配置好proxy代理了呀,只是我没找到原因为什么还会跨域
(ps:同样方法创建的vue3应用不会跨域)
贴一下代码:位置是:o2oa\o2web\source\x_component_financeassistreact\node_modules\@o2oa\react-scripts\config\o2componentProxySetup.js
[JavaScript] 纯文本查看 复制代码 const o2componentProxySetup = require('http-proxy-middleware');
const axios = require('axios');
const path = require('path');
const fs = require('fs/promises');
const p = path.resolve(process.cwd(), './o2.config.js');
const config = require(p);
const pkg = require(path.resolve(process.cwd(), './package.json'));
const componentPath = pkg.name;
const server = config.server;
const host = `${(server.https) ? 'https' : 'http'}://${server.host}${(!server.httpPort || server.httpPort==='80') ? '' : ':'+server.httpPort}`;
const myproxy = o2componentProxySetup.createProxyMiddleware({
target: host,
changeOrigin: true
});
module.exports = function (app) {
// console.log('host',host)
console.log('componentPath',componentPath)
app.use((req, res, next) => {
if (req.url.startsWith('/x_desktop/res/config/config.json')) {
console.log('1', 1)
console.log('req.url',req.url)
const configUrl = new URL(req.url, host);
console.log('configUrl', configUrl)
console.log('configUrl.toString()',configUrl.toString())
axios.get(configUrl.toString()).then((json)=>{
let o2Config = json.data;
o2Config.sessionStorageEnable = true;
o2Config.applicationServer = {
"host": (config.appServer && config.appServer.host) ? config.appServer.host : server.host
};
o2Config.center = [{
"port": server.port,
"host": server.host
}];
o2Config.proxyApplicationEnable = false;
o2Config.proxyCenterEnable = false;
res.json(o2Config);
console.log('o2Config',o2Config)
next();
}).catch(()=>{next()});
}else if (req.url.indexOf(componentPath+'/lp')!==-1 && req.url.indexOf('min')!==-1) {
console.log('2',2)
let toUrl = path.basename(req._parsedUrl.pathname).replace(/min\./, '')
toUrl = path.resolve(process.cwd()+'\\public', './lp/'+toUrl);
fs.readFile(toUrl).then((data)=>{
res.setHeader('Content-Type', 'application/javascript; charset=UTF-8');
res.send(data);
next();
}, ()=>{
res.send('');
next();
});
} else if (req.url.indexOf('/' + componentPath + '/') !== -1) {
console.log('3',3)
req.url = req.url.replace('/'+componentPath+'/', '/');
next()
} else {
console.log('4',4)
next();
}
});
app.use(
['^/o2_core', '^/o2_lib', '^/x_desktop', '^/x_component_*', '!^/'+componentPath],
myproxy
);
};
|