https://github.com/angular/angular-cli#proxy-to-backend以下是如何代理到后端的说明.我一步一步地做了所有事情,但请求仍然没有代理.
8080-My Express后端 4200-我的Angular 2前端
在Angular2项目中,我有文件proxy.conf.json
,内容如下:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
在angular2Package.json中,我将start
过程更改为"start": "ng serve --proxy-config proxy.conf.json"
当我输入inside commander 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
时,一切正常.在后端方面没有什么可寻找的.