https://github.com/angular/angular-cli#proxy-to-backend这里是一个如何代理后端的指令.我一步一步做了所有事情仍然没有代理请求.
8080 - 我的Express后端4200 - 我的Angular2前端
在Angular2项目中,我的文件proxy.cons.json内容如下:
{
"/api": {
"target": "http://localhost:8080",
"secure": false
}
}
Run Code Online (Sandbox Code Playgroud)
在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);
})
}
Run Code Online (Sandbox Code Playgroud)
我收到了一个错误http://localhost:4200/api/hello 404 (Not Found).我们可以看到,没有任何代理.为什么?我做错什么了吗?
要清楚.当我手动去http://localhost:8080/hello,一切正常.在后端方面没有什么可以寻找的.