Create-React-App 代理错误

mic*_*ael 2 node.js reactjs create-react-app

我正在创建一个 create-react-app。

我正在使用现有的 API(它是用 MVC C# web api 编写的)。

我希望 React 应用程序使用的 API 位于:https : //api.myserver.com.au

我正在从http://localhost运行 React 的开发版本

当我上线时,我将始终从与 API 不同的位置托管我的 React 应用程序。即我将在https://my.cool.app 上托管它,而 API 仍将在https://api.myserver.com.au

我的 package.json 如下:

{
  "name": "hbi.contractor",
  "version": "0.1.0",
  "private": true,
  "proxy": {
    "/api/*": {
      "target": "https://api.myserver.com.au"
    }
  },
  "dependencies": {
    "axios": "^0.17.1",
    "jquery": "^3.2.1",
    "materialize-css": "^0.100.2",
    "nodemon": "^1.14.11",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-form": "^2.16.1",
    "react-live-clock": "^2.0.2",
    "react-load-script": "0.0.6",
    "react-redux": "^5.0.6",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.17",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "react-materialize": "^1.1.2"
  }
}
Run Code Online (Sandbox Code Playgroud)

当我将代理设置为开发 MVC C# 本地主机服务器时,它工作正常,但是在实时 API 服务器和开发 React 上时,我尝试浏览到:

http://localhost:3000/api/Acccount/GetUser
Run Code Online (Sandbox Code Playgroud)

响应是:

Proxy error: Could not proxy request /api/Acccount/GetUser from localhost:3000 to https://api.myserver.com.au (ECONNRESET).
Run Code Online (Sandbox Code Playgroud)

在终端中,它与此消息非常相似:

Proxy error: Could not proxy request /api/Acccount/GetUser from localhost:3000 to https://api.myserver.com.au.
See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).

[HPM] Error occurred while trying to proxy request /api/Acccount/GetUser from localhost:3000 to https://api.myserver.com.au (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors
)
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以逐步调试节点(我假设是表示)服务器,以便我可以获得有关此错误的更多详细信息?

我打开了 fiddler,看起来没有任何访问远程代理服务器的尝试。

考虑到我计划在实时版本中保持这种方式,代理功能是否是正确的功能?

任何帮助,将不胜感激。

mic*_*ael 5

我已经通过更新我的 package.json 文件解决了这个问题:

  "proxy": {
    "/api/*": {
      "target": "https://api.hbiaustralia.com.au",
      "changeOrigin": true
    }
  },
Run Code Online (Sandbox Code Playgroud)

但我想知道在我的 Live 网站上使用“代理”有用吗?或者它是一种开发工具,应该以不同的方式创建实时站点?