在包json中添加多个代理

use*_*751 4 proxy node.js reactjs server

我为我的 React 应用程序使用了两台服务器,一台用于 express,另一台用于 create-react-app。所以在 package.json 的反应端服务器中,我添加了:

"proxy": {
    "/auth/google": {
      "target": "http://localhost:5000"
    }
  },
Run Code Online (Sandbox Code Playgroud)

我运行服务器,但出现此错误:

When specified, "proxy" in package.json must be a string.
Instead, the type of "proxy" was "object".
Either remove "proxy" from package.json, or make it a string.
Run Code Online (Sandbox Code Playgroud)

我该如何解决?如何添加代理?也许使用其他语法?

小智 5

create-react-app v2 中不推荐使用高级代理设置。如果您不使用字符串,但就像在您的情况下一样,使用对象,则必须使用http-proxy-middleware并在src/文件夹中设置setupProxy.js文件。 1. 首先,使用 npm 或 Yarn安装http-proxy-middleware

npm install http-proxy-middleware --save
# or
yarn add http-proxy-middleware
Run Code Online (Sandbox Code Playgroud)


2. 然后,创建src/setupProxy.js并添加以下内容:

const proxy = require('http-proxy-middleware')

module.exports = function(app) {
  app.use(proxy('auth/google', { target: 'http://localhost:5000/' }))
}
Run Code Online (Sandbox Code Playgroud)



有关此问题的更多信息:将高级代理配置移至 src/setupProxy.js