企业代理背后的角反向代理

时间:2018-10-19 17:45:23

标签: angular proxy

我尝试运行一个角度反向代理来请求API,就好像它是本地主机一样,但是我落后于公司代理。

我创建了proxy.conf.json文件

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

然后我以npm start方式运行(我修改了package.json)

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

¿有人知道我如何配置angular的公司代理吗?

我有这个错误。

[HPM] Error occurred while trying to proxy request

1 个答案:

答案 0 :(得分:0)

您是否尝试过documentation

中描述的内容
use std::borrow::Cow;

struct ReferenceHoldingStruct<'a> {
    pub prop: Cow<'a, str>
}

fn generate_these_1<'a>(input: &'a str) -> ReferenceHoldingStruct<'a> {
    ReferenceHoldingStruct { prop: Cow::Borrowed(input) }
}

fn generate_these_2<'a>(input: String) -> ReferenceHoldingStruct<'a> {
    ReferenceHoldingStruct { prop: Cow::Owned(input) }
}

创建proxy.js

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">

        <item name="android:typeface">monospace</item>
        <item name="android:background">@drawable/shape_rounded_bottom_sheet_dialog</item>
    </style>

    <style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheet</item>
    </style>

    <style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog" />


    <style name="BottomSheetTransparent" parent="@style/Widget.Design.BottomSheet.Modal">

        <item name="android:typeface">monospace</item>
        <item name="android:background">@color/fully_transparent</item>
    </style>

    <style name="BaseBottomSheetTransparentDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheetTransparent</item>
    </style>

    <style name="BottomSheetTransparentDialogTheme" parent="BaseBottomSheetTransparentDialog" />
</resources>

然后使用npm install --save-dev https-proxy-agent 进行修改(修改package.json)

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);