[英]How to configure a proxy in Angular CLI
我創建了一個新的 @angular/cli 項目並在根組件中添加了一個 http GET 調用。 如果不使用代理,這可以正常工作。
export class AppComponent implements OnInit {
constructor(private http: Http) {}
ngOnInit() {
this.http
.get('https://dog.ceo/api/breeds/list/all')
.map(response => response.json())
.subscribe(data => console.log(data));
}
}
當我嘗試按照Angular CLI wiki中的描述添加配置時,出現問題。
我的 proxy.conf.json 文件:
{
"/api": {
"target": "https://dog.ceo",
"secure": false
}
}
我將組件中的 URL 更改為/api/breeds/list/all
。 我跑了ng serve --proxy-config proxy.conf.json
然后我在瀏覽器控制台中收到內部服務器錯誤消息。 在我開始 ng serve 的終端中,我收到此消息[HPM] Error occurred while trying to proxy request /api/breeds/list/all from localhost:4200 to https://dog.ceo (EPROTO) (https://nodejs.org/api/errors.html#errors_common_system_errors)
我嘗試了其他幾個配置選項和多個 API。 結果相似。
完整代碼可在 GitHub 上獲得: https ://github.com/ErikNijland/angular-cli-proxy-test
使用的版本:
注意:我知道 @angular/cli 正在使用 Webpack。 這反過來又使用了 http-proxy-middleware。
我認為這是因為您從 http 詢問 https 的來源。 這可以使用代理中的標志來解決。
{
"/api": {
"target": "https://dog.ceo",
"secure": false,
"changeOrigin": true
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.