【发布时间】:2020-03-05 18:34:18
【问题描述】:
我正在按照这个基本指南来设置一个本地开发环境,同时运行 react 和 node。在 react 目录的 package.json 添加 "proxy": "http://localhost:4001" 语句后我被卡住了。它一直说:代理错误:无法将请求 /flower 从 localhost:51427 代理到 http://localhost:4001。
环境:不涉及身份验证。它只是一个样板 node.js 和 create-react-app 设置。 create-react-app 版本是 3.0.1。我使用的是 Mac。
我尝试了以下方法来解决这个问题:
- 我验证了服务器运行正常,并且 localhost:4001 确实提供了数据。
- 我检查了浏览器开发工具,发现错误是 GET http://localhost:51427/flower 500 (Internal Server Error)
- 我还在服务器的 package.json 中添加了一个“--ignore client”
- 我还尝试按照以下说明安装 http-proxy-middleware:https://create-react-app.dev/docs/proxying-api-requests-in-development/。
这是 react 的 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 页面是空白的,没有文字。控制台日志也确认没有收到任何数据。
【问题讨论】:
标签: node.js reactjs proxy localhost