我已经搜索了现有的线程,并按他们说的去做,但没有成功。
当我在localhost上访问go应用程序时:8081/它正在成功地加载index.html,但它没有找到js文件,所以没有显示任何内容,我试图用go提供一个react。问题在于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))
}

发布于 2022-05-17 23:37:03
请参阅此github链接:https://github.com/julienschmidt/httprouter/issues/7
我添加了第二个httprouter实例,并通过该实例输入静态文件,然后将第一个httprouter设置为第二个httprouter。
static := httprouter.New()
static.ServeFiles("/*filepath", http.Dir("static"))
router.NotFound = statichttps://stackoverflow.com/questions/72281438
复制相似问题