【发布时间】:2017-02-10 01:32:29
【问题描述】:
https://github.com/angular/angular-cli#proxy-to-backend 这里是如何做代理到后端的说明。我一步一步做了所有的事情,但仍然没有代理请求。
8080 - 我的 Express 后端 4200 - 我的 Angular2 前端
在 Angular2 项目中,我有文件 proxy.conf.json,内容如下:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
在 Angular2 package.json 中,我将 start 过程更改为 "start": "ng serve --proxy-config proxy.conf.json"
当我输入指挥官npm start 时,在开始时我可以看到Proxy created: /api -> http://localhost:8080。好吧,我想到目前为止还不错。
我正在尝试发送请求 (Angular2)
constructor(private http: Http) {
this.getAnswer();
}
getAnswer(): any {
return this.http.get("/api/hello")
.subscribe(response => {
console.log(response);
})
}
我收到http://localhost:4200/api/hello 404 (Not Found) 的错误。正如我们所看到的,没有任何东西被代理。为什么?我是不是做错了什么?
要清楚。当我手动转到http://localhost:8080/hello 时,一切正常。后端没什么可找的。p>
【问题讨论】:
-
我想知道一件事,你的工作网址是localhost:8080/hello,那为什么你把它指向localhost:8080/api/hello?你绕过它到你的快递服务器了吗?
-
在 proxy.cons.json 中我将 localhost:8080 设置为 /api,所以当我指向 /api/hello 时,我猜这意味着我指向 localhost:8080/hello。
-
我知道这很奇怪,但角度代理路径区分大小写。所以在 Angular 代理中定义“api”或“Api”是不同的。
标签: angular proxy angular-cli