我有一个调用公共API的项目。为了使其正常工作,我需要通过公司代理将所有调用路由到该API。我可以通过proxy.conf.js文件做到这一点:
const HttpsProxyAgent = require('https-proxy-agent');
const proxyConfig = [
{
context: '/public-api',
target: 'https://public-api.com',
secure: false,
changeOrigin: true,
pathRewrite: {
'^/public-api': ''
},
},
{
context: '/backend',
target: "http://localhost:8080",
// What to do here to ignore corporate proxy??
}
];
function setupForCorporateProxy(proxyConfig) {
// const proxyServer = process.env.http_proxy || process.env.HTTP_PROXY;
const proxyServer = process.env.npm_config_proxy;
if (proxyServer) {
const agent = new HttpsProxyAgent(proxyServer);
console.log('Using corporate proxy server: ' + proxyServer);
proxyConfig.forEach(function (entry) {
entry.agent = agent;
});
}
return proxyConfig;
}
module.exports = setupForCorporateProxy(proxyConfig);
公共api调用工作正常。但是,对我的普通后端的调用也通过公司代理进行,这不是必需的。如何将我的配置固定为仅在调用public-api时才通过代理?