react中配置setupProxy.js后localhost拒绝访问的问题

//低版本配置代码
const proxy = require('http-proxy-middleware')

module.exports = function(app){
  app.use(
    proxy('/api',{
      target:'http://localhost:5000',
      changeOrigin:true,
      pathRewrite:{'^/api':''}
    }),
    proxy('/api2',{
      target:'http://localhost:5001',
      changeOrigin: true,
      pathRewrite:{'^/api2':''}
    })
  )
}

修改为最新的配置,即可解决问题

//新版配置代码
const {createProxyMiddleware} = require('http-proxy-middleware')

module.exports = function(app){
  app.use(
    createProxyMiddleware('/api1',{
      target:'http://localhost:5000',
      changeOrigin:true,
      pathRewrite:{'^/api1':''}
    })
  )
}

 

posted @ 2022-02-11 13:37  土豆炖牛肉3  阅读(1893)  评论(1编辑  收藏  举报