Angular reverse proxy behind a corporate proxy

佐手、 提交于 2021-01-07 04:03:13

问题


I'm try run a angular reverse proxy to request a API as if it were localhost, but i'm behind a corporate proxy.

I created a proxy.conf.json file

{
  "/api/*": {
    "target": "http://104.43.135.128:8000",
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug",
    "pathRewrite": {"^/api" : ""}
  }
}

and i'm run angular with npm start (I modified the package.json)

"start": "ng serve --proxy-config proxy.conf.json",

¿Does anyone know how i canconfigure a corporate proxy in angular?

I have this error.

[HPM] Error occurred while trying to proxy request

回答1:


Have you tried to do what is descibed in the documentation

npm install --save-dev https-proxy-agent

Create proxy.js

var HttpsProxyAgent = require('https-proxy-agent');
var proxyConfig = [{
  context: '/api',
  target: 'http://104.43.135.128:8000',
  secure: false,
  changeOrigin: true,
  pathRewrite: { "^/api": ""}
}];

function setupForCorporateProxy(proxyConfig) {
  var proxyServer = process.env.http_proxy || process.env.HTTP_PROXY;
  if (proxyServer) {
    var agent = new HttpsProxyAgent(proxyServer);
    console.log('Using corporate proxy server: ' + proxyServer);
    proxyConfig.forEach(function(entry) {
      entry.agent = agent;
    });
  }
  return proxyConfig;
}

module.exports = setupForCorporateProxy(proxyConfig);

And run angular with npm start ( modify the package.json)

"start": "ng serve --proxy-config proxy.js",


来源:https://stackoverflow.com/questions/52897543/angular-reverse-proxy-behind-a-corporate-proxy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!