首页 > 解决方案 > 在包json中添加多个代理

问题描述

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

"proxy": {
    "/auth/google": {
      "target": "http://localhost:5000"
    }
  },

我运行服务器并收到此错误:

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.

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

标签: node.jsreactjsserverproxy

解决方案


在 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


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

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

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



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


推荐阅读