我如何在 Go 中提供 CSS 和 JS [英] How do I serve CSS and JS in Go

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

问题描述

我遵循了 Go 编写 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天全站免登陆