【问题标题】:how to proxy to backend server on certain path?如何代理到特定路径上的后端服务器?
【发布时间】:2016-01-19 22:30:41
【问题描述】:

这里是路线配置:

<Route path='/' component={CoreLayout}>
  <IndexRoute component={HomeView}/>
  <Route path='/404' component={NotFoundView}/>
  <Redirect from='*' to='/404'/>
</Route>

这是 webpack-dev-server 的代理配置:

proxy: {
  '/service': 'http://localhost:8080'
}

express 服务器监听 3000 端口。

我希望所有发送到http://localhost:3000/service 的请求都会被转移到http://localhost:8080,但是似乎react-router 处理了所有请求并且代理不起作用。

任何机构都知道如何解决这个问题?提前谢谢你

【问题讨论】:

  • 您找到解决方案了吗?

标签: react-router webpack-dev-server


【解决方案1】:
  1. 查看Webpack Dev Server文档,您需要为对象提供target属性。

  2. http-proxy-middleware 文档显示了使用模式进行匹配。

总之,我会试试这个:

proxy: {
  '/service/**': { target: 'http://localhost:8080' }
}

【讨论】:

    【解决方案2】:

    确保您正确处理 API 调用的承诺。我遇到了同样的问题,其中客户端服务器正在处理所有 API 调用,而不是返回 404 错误和代理接管。

    fetch("/api/list")
        .then(res => res.json())
        .then(data => {
          // handle the data
          })
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2017-02-09
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多