正确地提供一个CSS文件Golang

huangapple go评论94阅读模式
英文:

Properly serve a CSS file Golang

问题

我有一个CSS文件,我正在尝试在我的Web服务器上提供它(使用http包中的新功能)。我尝试过使用http.ServeFile和http.Handle。我还尝试将其作为模板进行提供,但问题是在HTML页面的底部打印了CSS文件的内容。有没有正确的方法在不打印整个CSS文件的情况下提供CSS文件?

英文:

I have a CSS file and I am trying to serve it on my Web Server (New with the http package). I have tried

http. (ServeFile, and Handle)

I have also tried serving it as a template, but the issue is that on the bottom of the HTML page, it prints the CSS. What is the proper way of serving the CSS file without it printing the whole CSS file on the bottom of the page?

答案1

得分: 0

你可以尝试这个替代方案。

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

如果这对你解决问题有帮助,请告诉我。

英文:

You can try this instead.

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

Let me know if it helped you to your problem.

答案2

得分: 0

我总是这样做:

http.HandleFunc("/game.css", serveCss)

以及这样:

func serveCss(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "client/game.css")
}

希望这对你有帮助。

英文:

I always do this:

http.HandleFunc("/game.css", serveCss)

and this:

func serveCss(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "client/game.css")
}

Hope this help

huangapple
  • 本文由 发表于 2017年8月30日 10:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/45951044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定