英文:
Load image and css in Golang
问题
我在项目的根目录下的server.js
文件中设置了一个路由。
http.HandleFunc("/",route.IndexHandler)
IndexHandler
在route
包中实现,代码如下:
func IndexHandler(w http.ResponseWriter, r *http.Request) {
data := struct {
Name string
}{
"My name",
}
util.RenderTemplate(w, "index", data)
}
RenderTemplate
函数在util
包中实现,代码如下:
func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
cwd, _ := os.Getwd()
t, err := template.ParseFiles(filepath.Join(cwd, "./view/"+tmpl+".html"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
项目的目录结构如下:
/
/public/css
/public/images
/public/js
/route
/view
index.html
视图文件位于view
文件夹中,路由器位于route
文件夹中。
在index.html
中,我使用以下方式包含资源:
<link rel="stylesheet" type="text/css" href="../public/css/style.css">
<img src="../public/images/img_landing_page_mac.png">
当请求适当的路径时,index.html
仍然被渲染,但是图片和样式表没有加载。我应该如何在Golang的HTML模板引擎中包含它们?
英文:
I setup a route in server.js
in package main
in root directory of project
http.HandleFunc("/",route.IndexHandler)
The IndexHandler
is implemented in package route
like this:
func IndexHandler(w http.ResponseWriter, r *http.Request) {
data:=struct{
Name string
}{
"My name",
}
util.RenderTemplate(w, "index", data)
}
The RenderTemplate
function is implemented in package util
like this:
func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
cwd, _ := os.Getwd()
t, err := template.ParseFiles(filepath.Join(cwd, "./view/" + tmpl + ".html"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
Directory structure in project like this:
/
/public/css
/public/images
/public/js
/route
/view
index.html
view is located in folder view
, router is in folder route
In index.html
I include resources like these:
<link rel="stylesheet" type="text/css" href="../public/css/style.css">
<img src="../public/images/img_landing_page_mac.png">
When request the appropriate path, index.html
is still rendered, but images and stylesheet are not loaded. How can I do to include them in Golang html template engine?
答案1
得分: 16
你需要明确要求服务器提供静态文件的服务。
请参考http.FileServer。
在你的情况下,注册另一个处理程序。
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
英文:
You need to explicitly ask your server to serve static files.
See http.FileServer
In your case register another handler.
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
答案2
得分: 3
像Aruna说的那样,注册一个静态文件服务器处理程序
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
要在HTML中使用这些文件,只需
<img src="/public/images/img_landing_page_mac.png">
英文:
Like Aruna said, register a static file server handle
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
And to use the files in your HTML, simply
<img src="/public/images/img_landing_page_mac.png">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论