初学者围棋问题
我有这个目录结构。
app_executable
html
|
- index.html
data
|
- static_file.json我无法让它服务于static_file.json in data/static_file.json。
func main() {
// this works and serves html/index.html
html := http.FileServer(http.Dir("html"))
http.Handle("/", html)
// this always 404's
data := http.FileServer(http.Dir("data"))
http.Handle("/data/", data)
fmt.Println("Listening on port " + port + "...")
log.Fatal(http.ListenAndServe(port, nil))
}任何帮助都是非常感谢的!
发布于 2014-12-23 20:59:38
问题是,FileServer处理程序实际上是在此路径上查找一个文件:
./data/data/static_file.json而不是
./data/statif_file.json如果您使第一个文件存在,您的代码将工作。你可能想要做的是:
data := http.FileServer(http.Dir("data"))
http.Handle("/", data)或
data := http.FileServer(http.Dir("data"))
http.Handle("/data/", http.StripPrefix("/data/", data))我会选择前者,因为这可能是你真正想做的。将处理程序附加到根,任何匹配的/data/都会按预期返回。
如果您查看从
data := http.FileServer(http.Dir("data"))你会发现它是
&http.fileHandler{root:"data"}它表示根位于./data,因此尝试在该根下找到一个与请求路径匹配的文件。在您的例子中,路径是data/staticfile.json,所以它最终会检查./data/data/staticfile.json(不存在)和404 s。
https://stackoverflow.com/questions/27627702
复制相似问题