Angular proxy.conf.js使用错误的网关

时间:2019-06-03 09:06:42

标签: angular proxy visual-studio-code angular-cli vscode-tasks

我有一个大型Angular应用程序,其中必须定义多个网关。

我在 proxy.con.js 文件中实现了此目标,并修改了 Visual Studio代码任务 angular.json 文件。 / p>

尽管如此,对于特定的API调用,总是使用错误的网关,而不是为其定义的网关。

我的proxy.conf.js看起来像这样:

var HttpsProxyAgent = require('https-proxy-agent');
var proxyConfig = [
  {
    context: '/api/**',
    target: process.env.gateway_1,
    changeOrigin: true,
    agent: new HttpsProxyAgent(process.env.http_proxy || process.env.HTTP_PROXY),
    secure: false,
    logLevel: 'debug'
  },
  {
    context: '/resources**',
    target: process.env.gateway_1,
    changeOrigin: true,
    agent: new HttpsProxyAgent(process.env.http_proxy || process.env.HTTP_PROXY),
    secure: false,
    logLevel: 'debug'
  },
  {
    context: '/api/v1/vehicles/**',
    target: process.env.gateway_2,
    changeOrigin: true,
    agent: new HttpsProxyAgent(process.env.http_proxy || process.env.HTTP_PROXY),
    secure: false,
    logLevel: 'debug'
  },
  {
    context: '/api/v1/vehicles**',
    target: process.env.gateway_2,
    changeOrigin: true,
    agent: new HttpsProxyAgent(process.env.http_proxy || process.env.HTTP_PROXY),
    secure: false,
    logLevel: 'debug'
  }
];

module.exports = proxyConfig;

angular.json内部的服务目标如下:

"serve": {
  "builder": "@angular-devkit/build-angular:dev-server",
  "options": {
    "browserTarget": "my-ui:build",
    "port": 8080,
    "ssl": true,
    "proxyConfig": "src/proxy.conf.js"
  },
 ...

package.json的脚本部分如下所示:

  "scripts": {
    "ng": "ng",
    "start": "ng serve --base-href=/ui/",
   ...

,而我的visual Studio代码任务在task.json中看起来像这样:

{
    "version": "2.0.0",
    "options": {
      "env": {
        "gateway_1": "https://gateway1.apps.de1.my-cloud.com",
        "gateway_2": "https://gateway2.apps.de1.my-cloud.com",
        "http_proxy": "http://localhost:3128",
      }
    },
    "tasks": [
      {
        "type": "shell",
        "label": "Start Application",
        "command": "npm run start"
      },
    ....

仅使用gateway_1。对于所有必须通过gateway_2的呼叫,我都会收到404。CLI还会显示错误的网关。

  

获取/ api / v1 / vehicles?Size = 100&type = xyz->   https://gateway 1 .apps.de1.my-cloud.com

代替

  

获取/ api / v1 / vehicles?Size = 100&type = xyz->   https://gateway 2 .apps.de1.my-cloud.com

我在做什么错了?

0 个答案:

没有答案