我正在尝试找到一种方法将某些变量传递给我的代理,以便我可以使用一些参数调用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和date以及一些静态设置,但是在控制台中它显示:
[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 返回请求格式错误 - 因为我缺少这些额外的变量。然后我如何将某些内容传递给代理,以便实际重定向如下所示:
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/
(在 -> 之后(。
由于这是您在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"即可完成工作。