httpRouter在提供静态文件时出现问题

huangapple go评论78阅读模式
英文:

Trouble with httpRouter serving static files

问题

我已经搜索了现有的线程,并按照它们的建议进行了尝试,但没有成功。

当我访问 localhost:8081/ 上的我的 Go 应用时,成功加载了 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 用户名和密码
	user := ""
	pass := ""

	DefaultUser()

	// HTTPRouter 设置和路由
	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))
}

httpRouter在提供静态文件时出现问题

英文:

I have searched for existing threads and did what they said to no success.

When I access my go app at localhost:8081/ it is successfully loading index.html but it is not finding the js file so nothing is displayed, I am trying to serve a react app with go. The problem is with the ServeFiles function as that is not working properly.

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))
}

httpRouter在提供静态文件时出现问题

答案1

得分: 0

请参考这个 GitHub 链接:https://github.com/julienschmidt/httprouter/issues/7

我添加了第二个 httprouter 实例,并通过它传递了静态文件,然后将第一个 httprouter 的 NotFound 设置为第二个 httprouter:

static := httprouter.New()
static.ServeFiles("/*filepath", http.Dir("static"))
router.NotFound = static
英文:

Refer to this github link: https://github.com/julienschmidt/httprouter/issues/7

I added a second httprouter instance, and fed the static files through that, then set the first httprouter NotFound to the second httprouter

static := httprouter.New()
static.ServeFiles("/*filepath", http.Dir("static"))
router.NotFound = static

huangapple
  • 本文由 发表于 2022年5月18日 06:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/72281438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定