英文:
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路径下提供磁盘上的目录,您可以使用StripPrefix
在FileServer
看到请求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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论