在您的情况下,仅需要构建处理程序。它必须指向目录而不是文件。除路由外,其他处理程序已过时。
package mainimport ( "fmt" "github.com/gorilla/mux" "log" "net/http" "time")func main() { r := mux.NewRouter() r.HandleFunc("/route1", index).Methods("GET") r.HandleFunc("/route2", index).Methods("GET") buildHandler := http.FileServer(http.Dir("client/build")) r.PathPrefix("/").Handler(buildHandler) srv := &http.Server{ Handler: r, Addr: "127.0.0.1:8080", WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } fmt.Println("Server started on PORT 8080") log.Fatal(srv.ListenAndServe())}func index(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "client/build/index.html")}仅标准库可以实现相同的目的。
package mainimport ( "fmt" "log" "net/http" "time")func main() { r := http.NewServeMux() r.HandleFunc("/route1", index) r.HandleFunc("/route2", index) buildHandler := http.FileServer(http.Dir("client/build")) r.Handle("/", buildHandler) srv := &http.Server{ Handler: r, Addr: "127.0.0.1:8080", WriteTimeout: 15 * time.Second, ReadTimeout: 15 * time.Second, } fmt.Println("Server started on PORT 8080") log.Fatal(srv.ListenAndServe())}func index(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "client/build/index.html")}


