【发布时间】:2016-10-25 02:16:51
【问题描述】:
我正在编写一个 Go 应用程序,它为两个不同目录中的文件提供服务。
我的项目结构:
PROJECT_DIR
PROJECT_DIR/config
PROJECT_DIR/src
PROJECT_DIR/client
PROJECT_DIR/client/node_modules
PROJECT_DIR/client/www
在我的主 go 文件中,我使用以下代码启动文件服务器:
func main() {
log.Print("started web server...");
httpsPortStr := ":" + strconv.FormatUint(config.CfgIni.HttpsPort, 10)
log.Printf("starting https web server at port %v", config.CfgIni.HttpsPort)
http.Handle("/", http.FileServer(http.Dir("client/www")))
http.Handle("/node_modules",http.FileServer(http.Dir(("client/node_modules"))))
err := http.ListenAndServeTLS(httpsPortStr, config.CfgIni.CertificateFile, config.CfgIni.PrivateKeyFile, nil)
if err != nil {
log.Fatalf("https server stopped with the following error: %v", err)
} else {
log.Print("https server stopped with no error")
}
}
如您所见,我将 / 映射到 client/www 并将 /node_modules 映射到 client/node_modules。
当我尝试访问客户端/www 上的文件时,例如 https://host:port/test.html,效果很好!
当我尝试访问 client/node_modules 上的文件时,例如:https://host:port/node_modules/test.html,我得到 404 page not found。
test.html 文件存在于这两个位置并且是可读的(没有权限问题)。
我可能以某种方式配置了错误的路由。
有什么想法吗?
谢谢!
【问题讨论】: