Keep getting "Proxy error: Could not proxy request" error after adding proxy to react package.json
Keep getting "Proxy error: Could not proxy request" error after adding proxy to react package.json
我正在按照这个基本指南设置一个本地开发环境,同时使用 React 和 Node 运行。添加 "proxy" 后我卡住了:“http://localhost:4001" statement to the package.json of the react directory. It keeps saying: Proxy error: Could not proxy request /flower from localhost:51427 to http://localhost:4001.
环境:不涉及身份验证。它只是一个样板 node.js 和 create-react-app 设置。 create-react-app 版本是 3.0.1。我正在使用 Mac.
我尝试了以下方法来解决这个问题:
- 我验证了服务器 运行 正确并且 localhost:4001 确实提供了数据。
- 我检查了浏览器开发工具,发现错误是 GET http://localhost:51427/flower 500(内部服务器错误)
- 我还在服务器的 package.json
中添加了一个“--ignore client”
- 我还尝试按照以下说明安装 http-proxy-middleware:https://create-react-app.dev/docs/proxying-api-requests-in-development/。
这是反应包 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"
}
react 的localhost 页面是空白的,没有文字。控制台日志也确认没有收到数据。
经过无数次的搜索,我找到了问题并解决了!
首先,由于代理错误在我 运行 react 的 npm 命令时出现,我认为我的 package.json 中的代理语句正在工作。因为我也可以通过 localhost:4001 到达服务器,问题一定是反应服务器以某种方式找不到 node.js 服务器,即不在同一个通信渠道等
然后我搜索并发现问题与它们有关,而不是同时 运行ning(我还考虑了其他可能性,例如 ipv4 上的 运行ning 与另一个 运行在 ipv6 上 ning 但这一个似乎是最可能的解决方案)。这个答案帮助我弄清楚如何实现它:。
但是并发请求会失败,因为当它尝试 运行 客户端时,它会失败,因为端口总是被阻塞。例如当我运行"npm start --prefix client"时,即使我在package.json中更改了端口,它也总是报告"something running on port XXX"。然后我发现问题必须没有为我的本地主机设置正确的配置,这个答案帮助我解决了问题:npm start reports "Something is already running on port XXX" no matter what XXX is。
现在并发,终于可以用了。
尝试在 package.json 的代理配置中添加 "secure":false。
package.json
...
"proxy":{
“/api”:{
"target": "https://localhost:5002",
"secure": 错误
}
},
...
参考Link:
https://github.com/facebook/create-react-app/issues/3823
我正在按照这个基本指南设置一个本地开发环境,同时使用 React 和 Node 运行。添加 "proxy" 后我卡住了:“http://localhost:4001" statement to the package.json of the react directory. It keeps saying: Proxy error: Could not proxy request /flower from localhost:51427 to http://localhost:4001.
环境:不涉及身份验证。它只是一个样板 node.js 和 create-react-app 设置。 create-react-app 版本是 3.0.1。我正在使用 Mac.
我尝试了以下方法来解决这个问题:
- 我验证了服务器 运行 正确并且 localhost:4001 确实提供了数据。
- 我检查了浏览器开发工具,发现错误是 GET http://localhost:51427/flower 500(内部服务器错误)
- 我还在服务器的 package.json 中添加了一个“--ignore client”
- 我还尝试按照以下说明安装 http-proxy-middleware:https://create-react-app.dev/docs/proxying-api-requests-in-development/。
这是反应包 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"
}
react 的localhost 页面是空白的,没有文字。控制台日志也确认没有收到数据。
经过无数次的搜索,我找到了问题并解决了!
首先,由于代理错误在我 运行 react 的 npm 命令时出现,我认为我的 package.json 中的代理语句正在工作。因为我也可以通过 localhost:4001 到达服务器,问题一定是反应服务器以某种方式找不到 node.js 服务器,即不在同一个通信渠道等
然后我搜索并发现问题与它们有关,而不是同时 运行ning(我还考虑了其他可能性,例如 ipv4 上的 运行ning 与另一个 运行在 ipv6 上 ning 但这一个似乎是最可能的解决方案)。这个答案帮助我弄清楚如何实现它:。
但是并发请求会失败,因为当它尝试 运行 客户端时,它会失败,因为端口总是被阻塞。例如当我运行"npm start --prefix client"时,即使我在package.json中更改了端口,它也总是报告"something running on port XXX"。然后我发现问题必须没有为我的本地主机设置正确的配置,这个答案帮助我解决了问题:npm start reports "Something is already running on port XXX" no matter what XXX is。
现在并发,终于可以用了。
尝试在 package.json 的代理配置中添加 "secure":false。
package.json
... "proxy":{ “/api”:{ "target": "https://localhost:5002", "secure": 错误 } }, ...
参考Link: https://github.com/facebook/create-react-app/issues/3823