Why is there an error when proxy specifies multiple domain names in package.json?

in the package.json of create-react-app, you want to set multiple proxies

{
  "proxy":{
    "/api": {
      "target": "http://0.0.0.89:7300",
      "ws": true
    },
    "/foo": {
      "target": "http://0.0.11.22:8848",
      "ws": true
    }
  }
}

then npm statr reports an error:

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.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! r-d-m@0.1.0 start: `react-scripts start`
npm ERR! Exit status 1

ask for help, thank you!

Sep.07,2021

if you are using React, please refer to the following article
https://github.com/facebook/c.

the summary is to install http-proxy-middleware, and move your proxy settings to src/setupProx.js. Such as the following:

const proxy = require('http-proxy-middleware')
 
module.exports = function(app) {
  app.use(proxy('/api', { target: 'http://localhost:5000/' }))
  app.use(proxy('/*.svg', { target: 'http://localhost:5000/' }))
}

the error message is very clear. The value of proxy in package.json can only be string,. You should write this part in the config file

.
Menu