Go Web服务器在哪里查找文件 [英] Where does Go web server look for the files
问题描述
我有一个简单的web应用程序,名为HttpServer.go的代码文件是:
package main
import(
net / http
)
func main(){
mux:= http.NewServeMux()
文件:= http.FileServer(http.Dir(/ public))
mux.Handle(/ static /,http.StripPrefix(/ static /,files))
server: =& http.Server {
Addr:localhost:8080,
Handler:mux,
}
server.ListenAndServe()
}
我已将此代码文件放在%GOPATH%/ src / first_app下,并且我 go安装
这个程序, first_app.exe
出现在%GOPATH%/ bin
当我启动网络服务器时,我访问了
http:// localhost:8080 / static / a.txt ,但 404(NOT FOUND)
抱怨没有找到a.txt。
我会问哪里应该我将目录放在 public和a.txt中。
您在
http.Dir
表达式。 / public
在您的案例中。 很可能您没有名为 / public
(因为这是我熟悉的所有操作系统上的非标准目录路径,并且我怀疑你没有创建它)。
更改 / public
以匹配放置文件的路径。
I have a simple web application, the code file which is named HttpServer.go is:
package main
import (
"net/http"
)
func main() {
mux := http.NewServeMux()
files := http.FileServer(http.Dir("/public"))
mux.Handle("/static/", http.StripPrefix("/static/", files))
server := &http.Server{
Addr: "localhost:8080",
Handler: mux,
}
server.ListenAndServe()
}
I have put the this code file under %GOPATH%/src/first_app, and I go install
this program, the first_app.exe
shows up in %GOPATH%/bin
When I startup the webserver,I accessed
http://localhost:8080/static/a.txt, but 404(NOT FOUND)
complains that a.txt is not found.,
I would ask where should I put the directory public and a.txt
It looks in the path you specify in your http.Dir
expression. /public
in your case.
Most likely you don't have a path called /public
on your system (since this is a non-standard directory path on all OSes I'm familiar with, and I suspect you haven't created it).
Change /public
to match the path where you put your files.
这篇关于Go Web服务器在哪里查找文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!