后端的Angular

编程入门 行业动态 更新时间:2024-10-09 02:31:34
本文介绍了后端的Angular-CLI代理不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

本文发布于:2023-11-28 06:31:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后端   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!