[英]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.