如何在Go Lang中为CSS和JS提供服务? [英] How do I serve CSS and JS in Go Lang

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

问题描述

我遵循GoLang教程



如何让我的应用程序服务于我需要的CSS和JavaScript?



问题有si已解决,这里是工作主要:

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


解决方案

您可以提供一个静态f iles就像这样:

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

将为您的 css code> / 。当然,你可以在你选择的任何路径上提供任何目录。



我可能会确保静态路径不受其他路径的影响,并使用类似。

  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 / css / filename.css ,当然它找不到。它会在 static 目录中寻找 css / filename.css ,这是正确的。


I followed the GoLang tutorial here 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

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

解决方案

You can serve a directory of static files like so:

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.

I would probably 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.

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

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