English 中文(简体)
不从事反应和 no
原标题:proxy not working for react and node

我与代理一建立了问题。

这是我的一揽子方案。 json file:

"scripts": {
    "client": "cd client && yarn dev-server",
    "server": "nodemon server.js",
    "dev": "concurrently --kill-others-on-fail "yarn server" "yarn client""
}

我的客户一揽子计划。 json file:

"scripts": {
    "serve": "live-server public/",
    "build": "webpack",
    "dev-server": "webpack-dev-server"
},
"proxy": "http://localhost:5000/"

我在服务器一侧安装了快车,在5000港运行。 每当我向服务器提出请求时,即:

callApi = async () => {
    const response = await fetch( /api/hello );
    const body = await response.json();
    // ... more stuff
}

请求总是要

“Picture<

难道有人可以指出,必须做些什么来解决这一问题,以便实际将请求延伸到5,000港?

最佳回答

I experienced this issue quite a few times, and I figured it s because of the cache. To solve the issue, do the following


Edit: @mkoe said that he was able to solve this issue simply by deleting the package-lock.json file, and restarting the app, so give that a try first. If that doesn t resolve it, then do the following.


  1. Stop your React app
  2. Delete package-lock.json file and the node_modules directory by doing
    rm -r package-lock.json node_modules
    in the app directory.
  3. Then do npm install in the app directory.

希望这确定了你的代理问题。

问题回答

反应申请在当地仍然有点:8080是因为海滩。 说明这一点,遵循以下步骤。

  1. Delete package-lock.json and node_modules in React app
  2. Turn off React Terminal and npm install all dependencies again on React App
  3. Turn back on React App and the proxy should now be working

这个问题长期以来一直是aunting;但是,如果你采取上述步骤,就应当正确在服务器上看到你的再读应用点。

This is how I achieved the proxy calls.

  1. Do not rely on the browser s network tab. Put consoles in your server controllers to really check whether the call is being made or not. For me I was able to see logs at the server-side. My node server is running on 5000 and client is running on 3000.

Network tab -

“Dev

服务器记录——

“server”/

  1. Check if your server is really running on the same path /api/hello through postman or browser. For me it was /api/user/register and I was trying to hit /api/user
  2. Use cors package to disable cross-origin access issues.

我把这个问题花了几个小时,我相信,在其他一些情况下,上述一些事情可能是原因。 然而,在我的案件中,我使用了Vite,我一直试图将我的代理添加到 Package.json 文档中,而应在<代码>vite.config.js文档中添加。 页: 1 这里点击到Vite s docs。

In the end, my code looks like this:

export default defineConfig({
  server: {
    proxy: {
      "/api": {
        target: "http://localhost:8000",
        secure: false,
      },
    },
  },
  plugins: [react()],
});

Is your client being loaded from http://localhost:8080?

页: 1 a 如果没有绝对的URL使用,将反映客户网页(即东道名称和港口)的主机。 http:// localhost:80<>/code> <代码>fetch api to infer that mothers the request to be made to the fundamental url of http:// localhost:80/api/hello

You will need to specify an absolute URL if you want to change the port like that. In your case that would be fetch( http://localhost:5000/api/hello );, although you probably want to dynamically build it since eventually you won t be running on localhost for production.

For me "proxy" = "http://localhost:5000 did not work because I was listening on 0.0.0.0 changing it to "proxy" = "http://0.0.0.0:5000 did work.

确保你们把它放在一揽子计划上。 json in soil (react) rather than on Pack.json in services-side (node).

This solution worked for me, specially if you re using webpack.

Go to your webpack.config.js > devServer > add the below

proxy: {        /api :  http://localhost:3000/ , },

This should work out.

阅读更多关于网络包裹 代理:

  1. you should set the proxy address to your backend server, not react client address.
  2. you should restart the client after changing package.json
  3. you should use fetch( /api/... ) (instead of fetch( http://localhost:8080/api/ ))

我试图通过使用这么多解决办法解决这一问题,但对我没有任何帮助。 经过大量研究,我找到了以下解决办法,解决了我的代理问题,并帮助我把我的前端与我的诺贝尔服务器连接起来。 这些步骤是:

  1. killed all the terminals so that I can stop frontend and backend servers both.
  2. Installed Cors on My Node server.js file.
npm install cors

并将这些线路添入服务器。 j) 档案

var cors = require( cors )

app.use(cors())

  1. Into package.json file of frontend or client folder, I added this line,
"proxy" : "http://127.0.0.1:my_servers_port_address_"

现在,一切工作都很出色。

如果你使用<代码>.env变量,也确保你检查这些变量。 因此,如果我在该页寻求解决办法。

I tried all the solutions, proposed here, but it didn t work. Then I found out, that I tried to fetch from root directory (i.e. fetch( / )) and it s not correct for some reason. Using fetch( /something ) helped me.

你的后附数据或档案和校对文件的反应应放在同一服务器夹内。

you must give proxy after the name.{"name":"Project Name", "proxy":"http://localhost:5000"} port should match with your backend s port.

If you are seeing your static react app HTML page being served rather than 404 for paths you want to proxy, see this related question and answer:

https://stackoverflow.com/a/51051360/345648

(这没有回答原来的问题,但找谷歌回答这个问题,我在这里或许会帮助像我这样的其他人。)

In my specific case, I had a both Node backend, and an inner folder with a React project. I tried @Harshit s answer, which didn t work, until I had two package.json files in my project, one in the outer folder, and one in my client (React) folder. I needed to set up the proxy in the inner package.json, and I needed to clear the cache in the inner folder.

我的问题是代理路线中的“当地人”部分。 我的电脑不承认“当地东道”,因此我用<条码>下载:http://127.0.0.1:<PORT_HERE>代替<条码>http:// localhost:<PORT_HERE>。

与此类似:

app.use( / , proxy(
     http://localhost:3000 , // replace this with  http://127.0.0.1:3000 
    { proxyReqPathResolver: (req) => `http://localhost:3000${req.url}` }
));`

对我来说,我只是通过停止服务器,即前线和后台,并重新启用,解决了这一问题。

Here is an opinion Don t use proxies, use fetch directly

不工作

fetch("/signup", {    
        method:"post", 
        headers:{
            "Content-Type":"application/json" 
        },
        body:JSON.stringify(
        {
            name:"",
            email:"",
            password:"",
        }
        )

浪费6小时后实际工作

fetch("http://localhost:5000/signup", {    //    https -> http
        // fetch("/signup", {    
        method:"post", 
        headers:{
            "Content-Type":"application/json"              },
        body:JSON.stringify(
        {
            name:"",
            email:"",
            password:"",
        }
        )

在我的案件中,问题是代理人突然停止工作。

在调查之后 我发现,我把编织物从胎盘中移走,造成问题。

这个问题已经解决。

The problematic structure: enter image description here

解决办法:

“

面临类似的问题。 我的代理人没有把重新启动反应堆连接起来,这把我的问题固定下来。

在我的案件中,这是因为打字。 我写道:“应用/json”、“(有小笔)而不是“Content-Type”:“应用/json”,

您应安装这一套设备:

npm 安装http://proxy-middleware -save

Refrense:

<>strong>TLDR 回答: 问题是,从第17.0号违约到IPv6而不是IPv4域解决办法的现代版本,你可能在不支持、限制或误编为IPv6(很可能是公司或学校)的环境中运作。

www.un.org/Depts/DGACM/index_french.htm 如果在2023年没有为你工作的任何答案(在撰写本报告时是25个,而现在没有涉及WHY,而且你是active,即你的配对是正确的,那么问题很可能是由于在执行英国国家航天中心决议时没有遵守IPv6而不是IPv4。 这项IPv6违约事件始于诺德第17.0.0版,你可以读到更多有关该事件的here

如果你在限制或移至IPv6支持的网络环境中工作,这将打破你对<条码>的当地东道/代码>将决心<条码>:(相当于<条码>127.0.0.1的IPv6)的链接错误,正如大家在这里抱怨的那样。 访问<代码>:http://[::1]:5000,载于 Chrome或不管怎样的浏览器,你会发现同样的错误。 但是,如果<代码>http://127.0.0.1:5000 这是你的问题。

固定点比较容易。 同IPv4共同解决的正义力量。 根据您的设置,可以采取多种方式做到这一点,但你不得不放弃<代码>proxy > 设置在 Package.json 上,并依赖诺德本土的<代码>dns模块。 鉴于这个问题涉及<代码>create-react-app app,而且我的问题也存在,我举一个例子说明我是如何在这里确定的。 但是,你可以以任何快递服务器来做到这一点。

如前所述,可在<条码>中删除<条码>proxy ,在<条码>上建立<条码>Proxy.js,在<条码>上存档(如here。 页: 1 模块通过<代码>npm。 然后,你基本上希望自己进行IPv4-forced DNS调查,并利用IPv4地址提出代理要求。 可在<条码>家庭参数上填写<条码>4。 方法:

const dns = require("dns");
const { createProxyMiddleWare } = require("http-proxy-middleware");

const targetHost = "localhost";
const targetPort = 5000;
const port = 3000;

module.exports = function (app) {
    dns.lookup(targetHost, { family: 4 }, (err, address) => {
        if (err) {
            console.error( DNS lookup failed );
            return;
        }
        const proxy = createProxyMiddleware("/api", {
            target: `http://${address}:${targetPort}`,
            changeOrigin: true,
        });
        app.use("/api", proxy);
    });
};

现在,如果你打上<代码>http:// localhost:3000/api/yourendpoint,这将转至http://127.0.0.1:5000。 http://[::1]:5000。 请注意,这是一个非常基本的例子,留下了错误处理,忽视了下述事实:ns食是一种不连贯的作业,但总体而言,如果你在IPv6没有适当工作的环境中重新工作,就应当这样做。





相关问题
How to change firefox proxy from webdriver?

how can I access Firefox proxy settings from Python Webdriver and change them to make Firefox use modified proxy settings without needing to restart it?

Regex Proxy Server

I control access via a proxy server and run some regex on every request. For prototype I have used curl, regex and php. Obviously this will not put up with any serious load. Can anyone suggest and ...

Specify a Proxy in config vs code for a WSE/SOAP web service

Is there a way to specify a WSE3 proxy in the config file instead of code. I figured out how to get it working in code as follows: valservice.Proxy = new System.Net.WebProxy("http://10.192.xx.xx:...

Accessing a git repository via ssh behind a firewall

I would like to access (clone/push/pull) a private (via ssh) git repository while behind a corporate firewall that only allows http proxy access. I have written a robust Java (daemon) program (based ...

Set proxy data for an app

I have a few .NET apps which use the HttpWebRequest. My gut says i can create a config or manifest file and set the proxy data in there and .NET will automatically load it. But i dont know if that ...

热门标签