我搜索了現有的線程并按照他們所說的做了沒有成功。當我在 localhost:8081/ 訪問我的 go 應用程序時,它成功加載了 index.html 但它沒有找到 js 文件,因此沒有顯示任何內容,我正在嘗試使用 go 服務一個反應應用程序。問題出在 ServeFiles 函數上,因為它無法正常工作。func RouteDefault(w http.ResponseWriter, r *http.Request, p httprouter.Params) { index := template.Must(template.ParseFiles("static/index.html")) index.Execute(w, nil)}func main() { // BasicAuth username and password user := "" pass := "" DefaultUser() // HTTPRouter Settings and Routes router := httprouter.New() router.ServeFiles("/static/*filepath", http.Dir("static")) router.GET("/", RouteDefault) router.GET("/dashboard/", RouteDefault) router.GET("/about/", RouteDefault) router.GET("/signout/", RouteDefault) router.POST("/login/", BasicAuth(RouteLogin, user, pass)) router.GET("/getusers/", JWTAuth(RouteGetUsers)) router.POST("/newuser/", JWTAuth(RouteNewUser)) router.POST("/deleteuser/", JWTAuth(RouteDeleteUser)) router.POST("/mypassword/", JWTAuth(RouteMyPassword)) router.POST("/upload/", JWTAuth(RouteUpload)) router.GET("/getgraphs/", JWTAuth(RouteGetGraphs)) router.POST("/graph/", JWTAuth(RouteGetGraph)) router.POST("/deletegraph/", JWTAuth(RouteDeleteGraph)) router.GET("/autologin/", JWTAuth(RouteAutoLogin)) handler := cors.AllowAll().Handler(router) fmt.Println(http.ListenAndServe(":8081", handler))}
1 回答

30秒到達戰場
TA貢獻1828條經驗 獲得超6個贊
參考這個github鏈接:https://github.com/julienschmidt/httprouter/issues/7
我添加了第二個 httprouter 實例,并通過它提供靜態文件,然后將第一個 httprouter NotFound 設置為第二個 httprouter
static := httprouter.New() static.ServeFiles("/*filepath", http.Dir("static")) router.NotFound = static
- 1 回答
- 0 關注
- 168 瀏覽
添加回答
舉報
0/150
提交
取消