【发布时间】:2015-12-18 00:40:00
【问题描述】:
我正在创建一个 SPA。 我正在尝试使用 index.html 响应所有请求 (我在前端处理路由)。
我的目录结构如下:
后台
-- main.go
前端
..(一些其他文件)..
-- index.html
整个项目位于“C:\Go\Projects\src\github.com\congrady\Bakalarka”
我的 main.go 文件如下所示:
package main
import (
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "../Frontend/index.html")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
当我运行我的 main.go 文件(使用 go run)时,我的 localhost 总是以“404 page not found”响应。 当我尝试使用 fmt 提供静态内容时,一切正常。
请帮忙,我在这个问题上卡了很长时间,我无法让它工作。 谢谢
【问题讨论】:
-
我试过你的代码,可以肯定的是,但它在这里工作。由于您使用的是相对路径,您是否确定您是从
Backend目录而不是您的项目根目录中运行应用程序?
标签: http web go webserver localhost