Bri*_*its 5

您遇到的问题已在此问题的答案中详细介绍。值得通读答案(尤其是已接受的答案),因为有多种选择(有不同的缺点)。

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

我怀疑对您来说最简单的解决方案可能是使用哈希历史(在接受的答案中涵盖)。

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