在这段代码中,”/static/”是什么意思?

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

What does /static/ mean in this code?

问题

我正在尝试在GO中提供静态HTML文件。这是我的main()函数中的代码:

http.Handle("/", http.FileServer(http.Dir("/static/")))
http.ListenAndServe(":8989", nil)

它可以工作,但我不明白static是什么意思!有人可以解释一下吗?

英文:

Im trying to serve static html file in GO.
This is how my code in main() looks like.

http.Handle("/", http.FileServer(http.Dir("/static/")))
http.ListenAndServe(":8989", nil)

It works but i dont understand what does static mean! Someone please explain.

答案1

得分: 1

这里的/static/是用于提供请求的目录路径。

根据你的设置,你可能希望将其设置为相对路径而不是绝对路径...

英文:

Here, /static/ is the path to the directory that will be used to serve the requests.

Depending on your setup, you proabably want to set it to a relative path instead of an absolute one…

答案2

得分: 1

这意味着每当处理根目录为root的文件系统的HTTP请求时,它会尝试提供http.Dir中声明的文件,该文件使用操作系统的文件系统实现。

这意味着每当访问您的Web服务器索引URL时,它将尝试提供操作系统/static/目录下的文件。

要在替代的URL路径下提供磁盘上的目录,您可以使用StripPrefixFileServer看到请求URL之前修改其路径。

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/your/directory/to/static/files"))))
http.ListenAndServe(":8989", nil)
英文:

This means that whenever you handle requests that serves HTTP requests with the contents of the file system rooted at root, it try to server files declared in http.Dir which uses the operating system's file system implementation.

This means that whenever you access your web server index url it will try to serve files under the operating system /static/ directory.

To serve a directory on disk under an alternate URL path you can use StripPrefix to modify the request URL's path before the FileServer sees it.

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("/your/directory/to/static/files"))))
http.ListenAndServe(":8989", nil)

huangapple
  • 本文由 发表于 2016年3月9日 19:24:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/35890248.html
匿名

发表评论

匿名网友

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

确定