简体   繁体   English

添加代理以反应 package.json 后,不断收到“代理错误:无法代理请求”错误

[英]Keep getting “Proxy error: Could not proxy request” error after adding proxy to react package.json

I am following this basic guide to set up a local dev environment with both react and node running.我正在按照这个基本指南来设置一个同时运行反应和节点的本地开发环境。 I am stuck after adding a "proxy": " http://localhost:4001 " statement to the package.json of the react directory.在向反应目录的package.json添加“代理”后,我被卡住了。 It keeps saying: Proxy error: Could not proxy request /flower from localhost:51427 to http://localhost:4001 .它一直说:代理错误:无法将请求 /flower 从 localhost:51427 代理到http://localhost:4001

Environment: There's no authentication involved.环境:不涉及身份验证。 It is just a boilerplate node.js and create-react-app setup.它只是一个样板 node.js 和 create-react-app 设置。 The create-react-app version is 3.0.1. create-react-app 版本是 3.0.1。 I am using a Mac.我正在使用 Mac。

I tried the following way to figure this problem out:我尝试了以下方法来解决这个问题:

  1. I verified that the server is running correctly and localhost:4001 does provide data back.我验证了服务器运行正常,并且 localhost:4001 确实提供了数据。
  2. I checked the browser dev tool and see the error is GET http://localhost:51427/flower 500 (Internal Server Error)我检查了浏览器开发工具,发现错误是 GET http://localhost:51427/flower 500 (Internal Server Error)
  3. I also added a "--ignore client" in the server's package.json我还在服务器的 package.json 中添加了一个“--ignore client”
  4. I also tried installing http-proxy-middleware per this instruction: https://create-react-app.dev/docs/proxying-api-requests-in-development/ .我还尝试按照以下说明安装 http-proxy-middleware: https://create-react-app.dev/docs/proxying-api-requests-in-development/

Here's the package json for react:这是用于反应的 package json:

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.11.0",
    "react-dom": "^16.11.0",
    "react-scripts": "3.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }, 
  "proxy": "http://localhost:4001"
}

The localhost page of react is blank instead of having text. react 的 localhost 页面是空白的,而不是有文本。 The console log also confirms no data is received.控制台日志还确认没有收到任何数据。

After countless searches, I figured out the problem and got it working!!!经过无数次搜索,我发现了问题并解决了问题!!!

First, since the proxy error was showing up when I ran the npm command on react, I figured that the proxy statement in my package.json was working.首先,由于当我在 react 上运行 npm 命令时出现代理错误,我认为我的 package.json 中的代理语句正在工作。 Since I could also reach the server through localhost:4001, the problem must be that the react server somehow couldn't find the node.js server, ie not in the same channel of communication etc.由于我也可以通过 localhost:4001 到达服务器,所以问题一定是反应服务器以某种方式找不到 node.js 服务器,即不在同一个通信渠道等。

I then searched and found the issue has to do with them not running concurrently (I also considered other possibility like one running on ipv4 vs the other one running on ipv6 but this one seems to be the most likely solution).然后我搜索并发现问题与它们没有同时运行有关(我还考虑了其他可能性,比如一个在 ipv4 上运行,另一个在 ipv6 上运行,但这个似乎是最有可能的解决方案)。 This answer helped me figure out how to make it happen: Could not proxy request /pusher/auth from localhost:3000 to http://localhost:5000 (ECONNREFUSED) .这个答案帮助我弄清楚了如何实现它: 无法将请求 /pusher/auth 从 localhost:3000 代理到 http://localhost:5000 (ECONNREFUSED)

But then the concurrent request would fail because when it tried to run the client, it would fail because a port would always be clogged.但是随后并发请求会失败,因为当它试图运行客户端时,它会失败,因为端口总是会被阻塞。 For example when i run "npm start --prefix client", even if I changed the port in package.json, it would always report "something running on port XXX".例如,当我运行“npm start --prefix client”时,即使我更改了 package.json 中的端口,它总是会报告“在端口 XXX 上运行的东西”。 I then figured out the issue has to do not having the right config set up for my localhost and this answer helped me out: npm start reports "Something is already running on port XXX" no matter what XXX is .然后我发现问题必须没有为我的本地主机设置正确的配置,这个答案帮助了我: npm start 报告“Something is already running on port XXX”无论 XXX 是什么

Now with concurrently, it is finally working.现在同时,它终于可以工作了。

Try adding "secure":false in your proxy config in package.json.尝试在 package.json 的代理配置中添加“安全”:false。

package.json package.json

... "proxy": { "/api": { "target": " https://localhost:5002 ", "secure": false } }, ... ...“代理”:{“/api”:{“目标”:“ https://localhost:5002 ”,“安全”:假}},...

Refer the Link: https://github.com/facebook/create-react-app/issues/3823参考链接: https://github.com/facebook/create-react-app/issues/3823

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

相关问题 反应,获取无法代理请求错误 - React, getting could not proxy request error 不定义代理但得到“当在 package.json 中指定“代理”时”错误 - Define no proxy but get 'When “proxy” is specified in package.json' error package.json 中的代理不影响获取请求 - Proxy in package.json not affecting fetch request 如何仅在POST请求中在package.json中设置代理? - How to set up proxy in package.json for POST request only? 在Windows上,package.json中的React Create App Proxy Agent在Windows上运行缓慢 - React create app proxy agent in package.json is slow on Windows 反应 | Package.Json | “代理”在 Google App Engine 中不起作用 - React | Package.Json | “proxy” Not Working in Google App Engine 反应,从package.json代理websocket获取本地计算机 - React, proxy to websocket from package.json get local machine Package.json替代React SSR生产应用程序的代理 - Package.json proxy alternative to React SSR production app React/Express 应用程序 - API 请求后端代理错误:无法代理请求 (ECONNRESET) - React/Express App - API Requests to Backend Proxy error: Could not proxy request (ECONNRESET) create-react-app/express 代理错误:无法代理请求(ECONNRESET) - create-react-app/express Proxy error: Could not proxy request (ECONNRESET)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM