问题描述

目前,我正在使用以下设置来服务我的React应用

  func main(){http.Handle("/",http.FileServer(http.Dir("./build/"))))http.HandleFunc("/my_api",处理程序)http.ListenAndServe(:8090&",无)} 

和前端

  function App(){返回 (<路由器>< div>< nav>< ul>< li><链接到="/>首页</Link></li>< li><链接到="/my_frontend_path"> MyPath</Link></li></ul></nav>< Switch><路由路径="//my_frontend_path">< MyPath/></Route><路由路径="/".<首页/></Route></Switch></div></路由器>);} 
 http://localhost:8090/my_frontend_path  404页面未找到 main 

接受的答案),因为存在多种选择(不利之处各不相同).

 http://localhost:8090/my_frontend_path  build  http.FileServer  my_frontend_path  404页面未找到

我怀疑最适合您的解决方案可能是使用哈希历史记录(在已接受的答案中找到).

 index.html  my_frontend_path  index.html 
  const FSPATH ="./build/";func main(){fs:= http.FileServer(http.Dir(FSPATH))http.HandleFunc("/my_api",func(w http.ResponseWriter,_ * http.Request){w.Write([] byte("API CALL"))})http.HandleFunc("/",func(w http.ResponseWriter,r * http.Request){//如果所请求的文件存在,则返回if;否则返回index.html(文件服务器默认页面)如果r.URL.Path!="/"{fullPath:= FSPATH +字符串.TrimPrefix(path.Clean(r.URL.Path),"/")_,错误:= os.Stat(fullPath)如果err!= nil {如果!os.IsNotExist(err){恐慌}//所请求的文件不存在,因此我们返回默认值(解析为index.html)r.URL.Path ="/"}}fs.ServeHTTP(w,r)})http.ListenAndServe(:8090&",无)} 
 index.html  favicon.ico 

Currently I am serving my react app using the following setup

func main() {
    http.Handle("/", http.FileServer(http.Dir("./build/")))
    http.HandleFunc("/my_api", handler)
    http.ListenAndServe(":8090", nil)
}

and frontend

function App() {
  return (
    <Router>
      <div>
        <nav>
          <ul>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/my_frontend_path">MyPath</Link>
            </li>
          </ul>
        </nav>

        <Switch>
          <Route path="/my_frontend_path">
            <MyPath />
          </Route>
          <Route path="/">
            <Home />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}
http://localhost:8090/my_frontend_path404 page not foundmain

The issue you are experiencing is covered in some detail in the answers to this question. Its worth reading through the answers (particularly the accepted one) because there are a range of options (with varying downsides).

http://localhost:8090/my_frontend_pathmy_frontend_pathbuildhttp.FileServer404  page not found

I suspect that the simplest solution for you may be to use hash history (covered in the accepted answer).

index.htmlmy_frontend_pathindex.html
const FSPATH = "./build/"

func main() {
    fs := http.FileServer(http.Dir(FSPATH))

    http.HandleFunc("/my_api", func(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("API CALL")) })
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        // If the requested file exists then return if; otherwise return index.html (fileserver default page)
        if r.URL.Path != "/" {
            fullPath := FSPATH + strings.TrimPrefix(path.Clean(r.URL.Path), "/")
            _, err := os.Stat(fullPath)
            if err != nil {
                if !os.IsNotExist(err) {
                    panic(err)
                }
                // Requested file does not exist so we return the default (resolves to index.html)
                r.URL.Path = "/"
            }
        }
        fs.ServeHTTP(w, r)
    })
    http.ListenAndServe(":8090", nil)
}
index.htmlfavicon.ico

这篇关于如何让golang重定向到前端路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!