如何在Go中投放CSS和JS [英] How do I serve CSS and JS in Go

查看:100
本文介绍了如何在Go中投放CSS和JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了开始编写Web应用程序教程,但是由于任何原因我都遇到了麻烦让该应用程序可以投放CSS和JS.如果我在没有Go服务器的情况下运行我的静态页面,则页面CSS可以正常工作.另一方面,当我运行Go服务器时,CSS无效.

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.

这是我的HTML外观:

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">
        

然后在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?

How do I get my Go application to serve the CSS and JavaScript I need?

此问题已解决,这是主要的工作原理:

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

推荐答案

http.Handle("/", http.FileServer(http.Dir("css/")))

将在/提供您的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"))))

jscss都放置在项目的目录static中.然后,这将在domain.com/static/css/filename.cssdomain.com/static/js/filename.js

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

StripPrefix方法会删除前缀,因此它不会尝试搜索例如在static/css/filename.cssstatic目录中,当然找不到.它将在static目录中寻找css/filename.css,这是正确的.

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.

这篇关于如何在Go中投放CSS和JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆