英文:
How do I serve CSS and JS in Go
问题
我按照《Go编写Web应用程序》教程进行操作,但出现了一些问题,无法使应用程序提供CSS和JS。如果我在不运行Go服务器的情况下运行静态页面,页面的CSS就能正常工作。然而,当我运行Go服务器时,CSS就无法正常工作。
这是我的HTML代码的大致样子:
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../assets/css/custom.css">
然后在body
标签下面:
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
我的文件树结构如下:
go-affect/
├── data
│ └── …
├── static
│ ├── css
│ │ └── …
│ └── js
│ └── …
├── tmpl
│ ├── edit.html
│ ├── index.html
│ └── view.html
└── main.go
我该如何让我的Go应用程序提供所需的CSS和JavaScript?
编辑:
问题已经解决,以下是工作正常的main
函数:
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/index/", makeHandler(indexHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
这是我使用的处理程序示例:
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
英文:
I followed the Go Writing Web Applications tutorial but for whatever reason I am having trouble getting the app to serve CSS and JS. If I run my static page without the Go server the page CSS works fine. When I run the Go server on the other hand the CSS just doesn't work.
Here is what my HTML sort of looks like:
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../assets/css/custom.css">
then under the body
tag:
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
My file tree looks like this:
go-affect/
├── data
│   └── …
├── static
│   ├── css
│   │   └── …
│   └── js
│   │   └── …
├── tmpl
│   ├── edit.html
│   ├── index.html
│   └── view.html
└── main.go
How do I get my Go application to serve the CSS and JavaScript I need?
EDIT:
The problem has since been solved, here is the working main:
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/index/", makeHandler(indexHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
Here is an example of the handlers I am using:
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
答案1
得分: 38
http.Handle("/", http.FileServer(http.Dir("css/")))
这段代码会将你的css
目录服务于根路径/
。当然,你可以选择将任何目录服务于任何路径。
你可能希望确保静态路径不会干扰其他路径,并使用以下代码:
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
将你的js
和css
文件都放置在项目的static
目录中。这样,它们将被服务于domain.com/static/css/filename.css
和domain.com/static/js/filename.js
。
StripPrefix
方法会移除前缀,这样它就不会尝试在static
目录中搜索static/css/filename.css
,当然,它是找不到的。它会在static
目录中搜索css/filename.css
,这是正确的做法。
英文:
http.Handle("/", http.FileServer(http.Dir("css/")))
Would serve your css
directory at /
. Of course you can serve whichever directory at whatever path you choose.
You probably want to make sure that the static path isn't in the way of other paths and use something like this.
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
Placing both your js
and css
in the directory static
in your project. This would then serve them at domain.com/static/css/filename.css
and domain.com/static/js/filename.js
The StripPrefix
method removes the prefix, so it doesn't try to search e.g. in the static
directory for static/css/filename.css
which, of course, it wouldn't find. It would look for css/filename.css
in the static
directory, which would be correct.
答案2
得分: 1
我将我的 Apache 服务器的 CSS 目录链接添加到模板文件的 head 部分中。我将任何 Go 应用程序使用的模板和数据文件保存在 Go 应用程序运行的目录下,即此示例中的 cgi-bin 目录。
模板使用来自我的 Apache 服务器 assets/css
目录的 CSS:
<link rel="stylesheet" href="/assets/css/main.css" />
英文:
I added a link to my apache servers css dir into the head section of my template files. I keep the template and data files used by any go application under dir that the go application is running from. In this instance cgi-bin.
The template uses the css from my apache server assets/css
directory :
<link rel="stylesheet" href="/assets/css/main.css" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论