如何代理到特定路径上的后端服务器? [英] how to proxy to backend server on certain path?

查看:29
本文介绍了如何代理到特定路径上的后端服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是路由配置:

<IndexRoute 组件={HomeView}/><Route path='/404' component={NotFoundView}/><重定向从='*'到='/404'/></路线>

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

代理:{'/service': 'http://localhost:8080'}

express 服务器监听 3000 端口.

我希望所有发送到 http://localhost:3000/service 的请求都会被转移到 http://localhost:3000/servicea href="http://localhost:8080" rel="noreferrer">http://localhost:8080,但是好像react-router处理了所有的请求,代理不工作.>

有人知道如何解决这个问题吗?提前致谢

解决方案

  1. 查看 Webpack Dev Server 文档,你需要为对象提供 target 属性.

  2. http-proxy-middleware 文档显示了用法用于匹配的模式.

总之,我会试试这个:

代理:{'/service/**': { 目标: 'http://localhost:8080' }}

Here is the routes config:

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

Here is the proxy config for webpack-dev-server:

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

The express server listens on 3000 port.

I hope that all the requests send to http://localhost:3000/service would be transferred to http://localhost:8080, but it seems that react-router handles all the requests and the proxy does not work.

Any body knows how to fix this? Thank you in advance

解决方案

  1. Check Webpack Dev Server documentation, you need to provide object with target property.

  2. http-proxy-middleware documentation shows usage of patterns for matching.

In conclusion, I would try this:

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

这篇关于如何代理到特定路径上的后端服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆