我正在尝试使用Golang和gin为我的api和前端编写代理。如果请求转到除“/api”之外的任何内容,我想代理到svelte服务器。如果去“/api/something”,我想在杜松子酒中处理它。目前我的代码是这样的。func proxy(c *gin.Context) { remote, err := url.Parse("http://localhost:3000") if err != nil { panic(err) } proxy := httputil.NewSingleHostReverseProxy(remote) proxy.Director = func(req *http.Request) { req.Header = c.Request.Header req.Host = remote.Host req.URL.Scheme = remote.Scheme req.URL.Host = remote.Host req.URL.Path = c.Param("proxyPath") } proxy.ServeHTTP(c.Writer, c.Request)}func main() { r := gin.Default() r.Any("/*proxyPath", proxy) r.Run(":8080")}现在,如果我去,我看到我的苗条的应用程序。但是,如果想要添加任何其他路由,我会收到一个错误http://localhost:8080panic: catch-all conflicts with existing handle for the path segment root in path '/*proxyPath'
- 1 回答
- 0 关注
- 135 浏览
添加回答
举报
0/150
提交
取消