为什么在package.json里proxy指定多个域名会报错?

在create-react-app的package.json里,想设置多个代理

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

然后npm statr报错:

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

求助,谢谢!!

阅读 4.9k
2 个回答
新手上路,请多包涵

如果是用React,请参考以下的文章
https://github.com/facebook/c...

总结是要安装http-proxy-middleware,然后将你的proxy设置搬到src/setupProx.js里。如以下:

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/' }))
}

错误信息写的很清楚了,在package.json的proxy的值只能为string,你应该把这一部分写到config文件中

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题