ECONNREFUSED用于从本地主机到另一个本地主机的代理请求 [英] ECONNREFUSED for proxy request from localhost to another localhost

查看:62
本文介绍了ECONNREFUSED用于从本地主机到另一个本地主机的代理请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是angel的新手,并且正在逐步学习教学视频.我已经在这个问题上停留了将近2个星期,并且花了很多时间在其他类似的论坛上寻找解决方案.我意识到这是一个普遍的错误,但是在尝试了几十种解决方案但没有成功之后,我想我也可以发表自己的经验,也许它可以为我们许多人似乎都遇到的这个问题提供一些启示.

问题

在跟随视频的"Angular Tutorial#13"部分(从1:42:00开始)时发生错误.我完全按照视频中的步骤进行操作,但是在运行"npm start"的终端中遇到以下错误消息:
[HPM]尝试代理请求/api/file时发生错误.php从localhost:4200到http://localhost:1234(ECONNREFUSED)
我的"start"的package.json脚本:"ng serve --proxy-config proxyconfig.json"而我的proxyconfig.json包含:

  {"/api":{"target":"http://localhost:1234",安全":错误,"changeOrigin":为true}} 

在CMD中,我导航到文件夹"intro2angular",该文件夹是"/test/api/file.php"的根目录,在运行之前我先运行
php -S localhost:1234 在VisualStudioCode的Powershell中 npm start .然后,我运行 npm start .代码编译成功.在Chrome中,如果我转到 http://localhost:1234/test/api/file.php ,则该文件将显示在浏览器中.但是,如果我转到 http://localhost:4200 并单击锚点,该锚点执行代码以请求 file.php ,则<代码> [HPM]尝试将请求/api/file.php从localhost:4200代理到http://localhost:1234(ECONNREFUSED)时发生错误错误显示在终端中.在Chrome开发人员工具的网络标签中,当我检查 file.php 的标题时,会看到以下内容:

 请求URL:http://localhost:4200/api/file.php请求方法:GET状态码:504网关超时远程地址:127.0.0.1:4200推荐人政策:降级时不推荐人 

原谅我在解释我遵循的每个步骤时都比较冗长,但是由于我看到过一些类似的论坛帖子,其中涉及在尝试使用代理时出现ECONNREFUSED错误,因此我认为可能需要包括所有次要细节来区分在这种情况下错误的来源.如果需要更多详细信息/代码,我很乐意将其包含在内.

到目前为止我尝试过的解决方案

请注意,这不是一个完整的列表,我尝试了其他几种解决方案,但这些解决方案似乎是最常见的解决方案.另外,我并不是说我不会再尝试这些解决方案,因为我愿意接受所有建议.

  1. 为angular应用程序和file.php指定随机的本地主机端口
  2. 使用 setTimeout .getData()函数调用添加延迟
  3. 禁用防火墙
  4. 包括选项"pathRewrite":{"^/api":"} proxyconfig.json

我非常感谢您解决此问题,因为我自己没有获得成功,在解决此问题之前,我无法继续进行角度教程.预先谢谢你.

解决方案

如果您使用的是Angular和内置的PHP服务器,则必须同时使用IP地址.类似于Angular的"target":"http://127.0.0.1:8000" +类似于 php -S 127.0.0.1:8000 -t public 在PHP方面.

在此处找到此提示:https://forum.freecodecamp.org/t/econnrefused-error-during-learn-angular-freecodecamp-org-video/245739

To begin, I'm new to angular and I'm following a tutorial video step by step. I've been stuck on this issue for nearly 2 weeks and have spent many hours looking for solutions in other similar forums. I realize this is a common error, but after trying a couple dozen or so solutions but finding no success I figure I may as well post my experience and maybe it can shed some light on this problem many of us seem to be having.

The Issue

The error occurs while following along for the "Angular Tutorial #13" part of the video which starts around 1:42:00. I follow all steps exactly as shown in the video but I encounter the following error message in the terminal in which I run "npm start" :
[HPM] Error occurred while trying to proxy request /api/file.php from localhost:4200 to http://localhost:1234 (ECONNREFUSED)
My package.json scripts for "start": "ng serve --proxy-config proxyconfig.json" and my proxyconfig.json contains:

{
    "/api":{
        "target": "http://localhost:1234",
        "secure": false,
        "changeOrigin": true
    }
}

In CMD I navigate to the folder "intro2angular" which is the root of "/test/api/file.php" and I run
php -S localhost:1234 prior to running npm start in the powershell in VisualStudioCode. Then I run npm start. Code compiles successfully. In Chrome, if I go to http://localhost:1234/test/api/file.php then the file is displayed in the browser. However, if I go to http://localhost:4200 and click an anchor which executes the code to request file.php then the [HPM] Error occurred while trying to proxy request /api/file.php from localhost:4200 to http://localhost:1234 (ECONNREFUSED) error is displayed in terminal. In the Network tab of Developer Tools in Chrome, when I check the Headers for file.php I see the follow:

Request URL: http://localhost:4200/api/file.php
Request Method: GET
Status Code: 504 Gateway Timeout
Remote Address: 127.0.0.1:4200
Referrer Policy: no-referrer-when-downgrade

Forgive me for being verbose in explaining every step I followed, but since I've seen several similar forum posts regarding the ECONNREFUSED error being raised while trying to use a proxy, I thought that including every minor detail may be necessary to distinguish the source of the error in this instance. If any more details/code is necessary I'm more than happy to include it.

Solutions I've tried thus far

Note that this is not a complete list, I've tried several other solutions but these seem to be the most common which have worked for others. Also, I'm not saying that I wouldn't give these solutions another try as I am open to any and all suggestions.

  1. specifying random localhost ports for both the angular app and file.php
  2. using setTimeout to add a delay to the .getData() function call
  3. disabling firewall
  4. including the option "pathRewrite": {"^/api" : ""} to proxyconfig.json

I appreciate any and all help/suggestions/discussions around this issue as I've had no success on my own and I am unable to continue with the angular tutorial until this issue is resolved. Thank you in advance.

解决方案

If you are using Angular and PHP built in server, you have to use IP addresses on both sides. So something like: "target": "http://127.0.0.1:8000" on Angular side + something like php -S 127.0.0.1:8000 -t public on PHP side.

Found this tip here: https://forum.freecodecamp.org/t/econnrefused-error-during-learn-angular-freecodecamp-org-video/245739

这篇关于ECONNREFUSED用于从本地主机到另一个本地主机的代理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆