如何将变量传递给代理服务器url?

How to pass variables to proxy server url?

我正在尝试找到一种方法将某些变量传递给我的代理,以便我可以使用一些参数调用 API,就像下面的示例一样。

我的代理配置如下所示:

{
    "/darksky/*": {
    "target": "https://api.darksky.net/forecast/APIKEY/",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true
  }
}

在服务中我这样调用这个代理:

return this.http.get(`/darksky/${x},${y},${date}?exclude=hourly,daily,flags&units=auto`);

如您所见,我正在尝试将某些变量传递给 url - x、y 和日期以及一些静态设置,但在控制台中它显示:

[HPM] GET /darksky/53.1219648,18.0002529,2019-12-04T12:00:00?exclude=hourly,daily,flags&units=auto -> https://api.darksky.net/forecast/APIKEY/

并且 api returns 请求格式错误 - 因为我缺少这些额外的变量。那么我怎样才能将某些东西传递给代理,以便实际的重定向看起来像这样:

https://api.darksky.net/forecast/APIKz/53.1219648,18.0002529,2019-12-04T12:00:00?exclude=hourly,daily,flags&units=auto

编辑:angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "fe": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/fe",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": [
              "node_modules/alertifyjs/build/alertify.min.js"
            ]
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "fe:build",
            "proxyConfig": "src/app/proxyServer/proxy.config.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "fe:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "fe:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "fe:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "fe:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "fe"
}

你的控制台输出是

[HPM] GET /darksky/53.1219648,18.0002529,2019-12-04T12:00:00?exclude=hourly,daily,flags&units=auto -> https://api.darksky.net/forecast/APIKEY/

这是正确的,在箭头 (->) 之后,日志显示 GET 请求

/darksky/53.1219648,18.0002529,2019-12-04T12:00:00?exclude=hourly,daily,flags&units=auto

是主机的代理

https://api.darksky.net/forecast/APIKEY/ (after ->).

因为这是您在 proxy.conf.json 中提到的内容,它的工作方式与您所写的一样。

这是一个请求问题,您的最终请求是

https://api.darksky.net/forecast/APIKEY/darksky/53.1219648,18.0002529,2019-12-04T12:00:00?exclude=hourly,daily,flags&units=auto

这是错误的,根据 Darksky api,darksky 关键字不应出现在请求中。

您的要求应该是

https://api.darksky.net/forecast/APIKEY/53.1219648,18.0002529,2019-12-04T12:00:00?exclude=hourly,daily,flags&units=auto

@Plochie 指出了这个问题,我设法找到了解决方案:

{
    "/darksky/*": {
    "target": "https://api.darksky.net/forecast/APIKEY",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {"^/darksky" : ""}
  }
}

只需将 "pathRewrite" 添加到 "proxy.config.json" - 就像上面的代码片段一样。