简体   繁体   English

代理错误:无法将请求从 localhost:3000 代理到 http://localhost:44381/ (ECONNREFUSED)

[英]Proxy error: Could not proxy request from localhost:3000 to http://localhost:44381/ (ECONNREFUSED)

I am developing a Single Page Application (SPA) with ReactJS and ASP.NET.我正在使用 ReactJS 和 ASP.NET 开发单页应用程序 (SPA)。 II am trying to execute an HTTP Request to an endpoint from client side code.我正在尝试从客户端代码对端点执行 HTTP 请求。 I used the default ASP.NET SPA template and changed my configuration as shown below.我使用了默认的 ASP.NET SPA 模板并更改了我的配置,如下所示。

            fetch(`/items/${id}`).then((response: any) => {
                return response.data;
            })

I modified the default web api template and use a Proxy Server in development instead.我修改了默认的 web api 模板并在开发中使用代理服务器。 Like so:像这样:

            app.UseSpa(spa =>
             {
                spa.Options.SourcePath = "Client";

                if (env.IsDevelopment())
                {
                    spa.UseProxyToSpaDevelopmentServer("http://127.0.0.1:3000/");
                    //spa.UseReactDevelopmentServer(npmScript: "start");
                }
            });

When I execute the fetch in React client code, I get this error: Proxy error: Could not proxy request /items/1 from localhost:3000 to http://localhost:44381/ (ECONNREFUSED).当我在 React 客户端代码中执行 fetch 时,我收到此错误: Proxy error: Could not proxy request /items/1 from localhost:3000 to http://localhost:44381/ (ECONNREFUSED).

How do I configure my server-side code to proxy the request?如何配置我的服务器端代码来代理请求?

According to the linked issue on GitHub, the spa.UseReactDevelopmentServer should already setup the proxy server itself as of .Net Core 3.1.根据 GitHub 上的链接问题,从 .Net Core 3.1 开始, spa.UseReactDevelopmentServer应该已经设置了代理服务器本身。 So you might not need to be calling the spa.UseProxyToSpaDevelopmentServer yourself manually.因此,您可能不需要自己手动调用spa.UseProxyToSpaDevelopmentServer

https://github.com/dotnet/AspNetCore.Docs/issues/12008#issuecomment-573453266 https://github.com/dotnet/AspNetCore.Docs/issues/12008#issuecomment-573453266

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 - 反应:无法将请求代理到 http://localhost:3000/,因为对代理目标的请求失败 - ASP.NET Core - React: Failed to proxy the request to http://localhost:3000/, because the request to the proxy target failed 为什么 angular 向 https://localhost:4200 而不是 http://localhost:5001 发出 http 请求以从 Restful Asp.net 获取数据? - why angular makes http request to https://localhost:4200 instead of http://localhost:5001 to get data from Restful Asp.net? GET http://www.localhost:51644 / users net :: ERR_NAME_NOT_RESOLVED-$ http角度请求错误 - GET http://www.localhost:51644/users net::ERR_NAME_NOT_RESOLVED - $http angular request error HTTP错误500.19-内部服务器错误localhost - HTTP Error 500.19 - Internal Server Error localhost ASP.Net:尝试处理http:// localhost:5957 / http://yahoo.com时出现HTTP 400 Bad Request错误 - ASP.Net: HTTP 400 Bad Request error when trying to process http://localhost:5957/http://yahoo.com HTTP 错误 403.14 - 禁止,本地主机:12401 - HTTP Error 403.14 - Forbidden, localhost:12401 通过隧道(使用 ngrok)将 localhost 暴露给 inte.net:HTTP 错误 400:错误请求; 无效的主机名 - Exposing localhost to the internet via tunneling (using ngrok): HTTP error 400: bad request; invalid hostname 网络请求失败 - 从 localhost ReactNative 获取 - Network request failed - fetching from localhost ReactNative Yahoo,OAuth http请求。 在本地主机上工作,但不在实时服务器上 - Yahoo, OAuth http request. Works on localhost but not live server 如何判断请求是否来自代理? - How to tell if a Request is coming from a Proxy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM