github/angular/angular-cli#proxy-to-backend这是有关如何进行后端代理的说明.我一步一步地完成了所有工作,但仍然没有收到请求.
github/angular/angular-cli#proxy-to-backend here is an instruction how to do proxying to backend. I did everything step by step and still requests aren't proxied.
8080-我的Express后端 4200-我的Angular2前端
8080 - my Express backend 4200 - my Angular2 frontend
在Angular2项目中,我有一个文件proxy.cons.json,其内容如下:
In Angular2 project I have file proxy.cons.json with content like this:
{ "/api": { "target": "localhost:8080", "secure": false } }在Angular2 package.json中,我将start过程更改为"start": "ng serve --proxy-config proxy.conf.json"
In Angular2 package.json I changed start procedure to "start": "ng serve --proxy-config proxy.conf.json"
当我在指挥官npm start中键入内容时,一开始我会看到Proxy created: /api -> localhost:8080.好吧,到目前为止我认为还不错.
When I type inside commander npm start then at the start I can see Proxy created: /api -> localhost:8080. Well, so far is good I guess.
我正在尝试发送请求(Angular2)
I'm trying to send a request (Angular2)
constructor(private http: Http) { this.getAnswer(); } getAnswer(): any { return this.http.get("/api/hello") .subscribe(response => { console.log(response); }) }我遇到了错误localhost:4200/api/hello 404 (Not Found).如我们所见,没有什么可以替代的.为什么?我做错什么了吗?
I'm getting an error that localhost:4200/api/hello 404 (Not Found). As we can see, nothing has been proxied. Why? Did I do something wrong?
要明确.当我手动转到localhost:8080/hello时,一切正常.在后端没有什么可找的.
To be clear. When I go manually to localhost:8080/hello, all works fine. There is nothing to look for in backend side.
推荐答案您可以尝试以下方法:
{ "/api": { "target": "url", "secure": false, "pathRewrite": {"^/api" : ""} } }它对我有用,
** NG Live Development Server is running on localhost:4200. ** 10% building modules 3/3 modules 0 active[HPM] Proxy created: /api -> ec2-xx-xx-xx-xx.ap-south-1pute.amazonaws [HPM] Proxy rewrite rule created: "^/api" ~> ""更多推荐
后端的Angular
发布评论