添加代理以响应package.json后,出现“代理错误:无法代理请求”错误

问题描述 投票:0回答:1

我正在遵循本基本指南来设置一个本地开发环境,同时运行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。

我尝试了以下方法来解决此问题:

  1. 我已验证服务器运行正常,并且localhost:4001确实提供了数据。
  2. 我检查了浏览器开发工具,发现错误是GEThttp://localhost:51427/flower500(内部服务器错误)
  3. 我还在服务器的package.json中添加了“ --ignore client”
  4. 我也尝试按照以下说明安装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
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.