英文:
CSS file doesn't get referenced
问题
我有这段代码作为我的myApp.go:
package fastaticapp
import (
"html/template"
"log"
"net/http"
)
func init() {
http.HandleFunc("/", rootHandler)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
if r.URL.Path != "/" {
errorHandler(w, r, http.StatusNotFound, "")
return
}
page := template.Must(template.ParseFiles(
"static/_base.html",
"static/index.html",
))
if err := page.Execute(w, nil); err != nil {
errorHandler(w, r, http.StatusInternalServerError, err.Error())
return
}
}
目录布局如下:
webapp/
myApp/
server.go
static/
index.html
_base.html
img/
css/
main.css
我正在使用template包和Must函数。运行开发服务器时,浏览器会渲染HTML页面,并使用开发者工具查看HTML内容。问题是css文件夹中的main.css文件似乎没有被服务器正确处理。
对此有什么想法将不胜感激。
英文:
I have this code as my myApp.go:
package fastaticapp
import (
"html/template"
"log"
"net/http"
)
func init() {
http.HandleFunc("/", rootHandler)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))
if r.URL.Path != "/" {
errorHandler(w, r, http.StatusNotFound, "")
return
}
page := template.Must(template.ParseFiles(
"static/_base.html",
"static/index.html",
))
if err := page.Execute(w, nil); err != nil {
errorHandler(w, r, http.StatusInternalServerError, err.Error())
return
}
}
And the direcotry Layout looks like this:
webapp/
myApp/
server.go
static/
index.html
_base.html
img/
css/
main.css
I am using template package with Must function. running the dev server, the HTML page gets rendered by the browser and using developer tools I can see the html contents. The problem is main.css file within the css folder does not appear to be properly handled by the server.
Any thought on this would be highly appreciated.
答案1
得分: 2
请使用http.Dir("static/css")
代替http.Dir("css")
。
英文:
Use
http.Dir("static/css")
instead of
http.Dir("css")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论