小编Sas*_*aju的帖子

如何为 React 服务

我有一个简单的 React 应用程序,我想从我的 Go 服务器后端提供服务。我听说这个过程类似于提供静态 html 文件,但我似乎无法让它工作。

当我尝试在浏览器上查看该应用程序时,它显示“此页面无法正常工作”并且“本地主机已重定向太多次”

这是我在本地运行服务器以及尝试处理反应应用程序的代码

func main() {

r := mux.NewRouter()

// handle app
buildHandler := http.FileServer(http.Dir("./client/build/index.html"))
r.PathPrefix("/").Handler(buildHandler)

staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./client/build/static")))
r.PathPrefix("/static/").Handler(staticHandler)

r.HandleFunc("/", index).Methods("GET")

srv := &http.Server{
    Handler:      r,
    Addr:         "127.0.0.1:8080",
    WriteTimeout: 15 * time.Second,
    ReadTimeout:  15 * time.Second,
}

// serve
fmt.Println("Server started on PORT 8080")
log.Fatal(srv.ListenAndServe())


}
Run Code Online (Sandbox Code Playgroud)

这是索引路由的代码

func index(w http.ResponseWriter, r *http.Request) {
    // not sure if this is necessary
    http.ServeFile(w, r, "index.html")
}
Run Code Online (Sandbox Code Playgroud)

我相信解决方案很简单,而且我很可能在某处犯了一个小错误。

go mux reactjs

5
推荐指数
1
解决办法
1024
查看次数

标签 统计

go ×1

mux ×1

reactjs ×1