英文:
Go Serve static files , contents with router
问题
我正在尝试提供包含JavaScript、CSS和HTML文件的静态文件服务。
但是在static
目录中加载所有外部文件时出现了问题。
我做错了什么?
请帮助我。
以下是你的代码:
router := httprouter.New()
handler := func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
type Page struct {
Title string
}
tp := template.Must(template.ParseFiles("templates/main.html", "templates/base.html"))
tp.ExecuteTemplate(w, "base", &Page{Title: "AAAAA"})
}
router.Handle("GET", "/", handler)
router.Handle("GET", "/aaa", aaa.aaaHandler)
router.Handle("POST", "/aaa_01_submit", aaa.aaa01Submit)
router.Handle("GET", "/aaa_01_run", aaa.aaa01Run)
http.Handle("/static", http.FileServer(http.Dir("static")))
http.ListenAndServe(":8000", router)
这是你的文件结构:
/app
/templates
main.html
base.html
/static
/js
要读取的文件...
/lib
/css
main.go
请注意,你的代码中使用的路由器是httprouter
,而不是标准库中的http.ServeMux
。确保你已经正确导入了httprouter
包,并且在代码中进行了相应的处理。
另外,请确保你的文件路径是正确的,并且文件在相应的目录中可用。如果文件路径不正确,或者文件不存在,那么加载外部文件时就会失败。
如果问题仍然存在,请提供更多的错误信息或日志,以便我可以更好地帮助你解决问题。
英文:
I am trying to serve static files which include javascript , css , html files
But it is failing to load all the external files in static
directory
What did I do wrong?
Please help me
router := httprouter.New()
handler := func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
type Page struct {
Title string
}
tp := template.Must(template.ParseFiles("templates/main.html", "templates/base.html"))
tp.ExecuteTemplate(w, "base", &Page{Title: "AAAAA"})
}
router.Handle("GET", "/", handler)
// func (r *Router) Handle(method, path string, handle Handle)
// func (r *Router) Handler(method, path string, handler http.Handler)
// func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)
router.Handle("GET", "/aaa", aaa.aaaHandler)
router.Handle("POST", "/aaa_01_submit", aaa.aaa01Submit)
router.Handle("GET", "/aaa_01_run", aaa.aaa01Run)
http.Handle("/static", http.FileServer(http.Dir("static")))
http.ListenAndServe(":8000", router)
Here's my files
/app
/templates
main.html
base.html
/static
/js
files to read...
/lib
/css
main.go
答案1
得分: 4
问题出在以下几行代码上:
http.Handle("/static", http.FileServer(http.Dir("static")))
http.ListenAndServe(":8000", router)
第一行代码将静态文件处理程序注册到默认的 mux上。第二行代码使用 router
作为根处理程序来运行服务器。默认的 mux 和注册在其中的静态文件处理程序被忽略了。
有两种方法可以解决这个问题:
-
使用 ServeFiles 配置
router
来处理静态文件。router.ServeFiles("/static/*filepath", http.Dir("static"))
-
将
router
注册到默认的 mux 上,并将默认的 mux 作为根处理程序。此外,在 "/static" 后面添加一个斜杠,以便为整个目录树提供服务,并使用 StripPrefix 来去除文件服务器的 "/static/" 前缀。http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/", router) http.ListenAndServe(":8000", nil)
这些建议假设你正在使用类似 "/static/js/example.js" 的 URI 来提供静态资源。如果你使用的是类似 "/js/example.js" 的 URI,那么你需要单独注册每个静态资源目录。
英文:
The problem is on these lines:
http.Handle("/static", http.FileServer(http.Dir("static")))
http.ListenAndServe(":8000", router)
The first line registers the static file handler with the default mux. The second line runs the server with the root handler set to router
. The default mux and the static file handler registered with it are ignored.
There are a two ways to fix this:
-
Configure
router
to handle the static files using ServeFiles.router.ServeFiles("/static/*filepath", http.Dir("static"))
-
Register
router
with the default mux and use default mux as the root handler. Also, add a trailing "/" to "/static" to serve the entire tree and strip the "/static/" prefix for the file server.http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/", router) http.ListenAndServe(":8000", nil)
These suggestions assume that you are serving the static resources using the URIs like "/static/js/example.js". If you are using URIs like "/js/example.js", then you need to register each of the directories in static individually.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论