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
,一切正常。后端没有什么可寻找的。
你能尝试一下这个吗:
{
"/api": {
"target": "http://url.com",
"secure": false,
"pathRewrite": {"^/api" : ""}
}
}
这个对我有用,
** NG Live Development Server is running on http://localhost:4200. **
10% building modules 3/3 modules 0 active[HPM] Proxy created: /api -> http://ec2-xx-xx-xx-xx.ap-south-1.compute.amazonaws.com
[HPM] Proxy rewrite rule created: "^/api" ~> ""
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)