我正在编写一个在两个不同目录中提供文件的 Go 应用程序。我的项目结构:PROJECT_DIRPROJECT_DIR/configPROJECT_DIR/srcPROJECT_DIR/clientPROJECT_DIR/client/node_modulesPROJECT_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")}}如您所见,我映射/到客户端/www 和/node_modules客户端/node_modules。当我尝试访问 client/www 上的文件时,例如https://host:port/test.html,效果很好!当我尝试访问 client/node_modules 上的文件时,例如:https://host:port/node_modules/test.html,我找不到 404 页面。test.html 文件存在于这两个位置并且是可读的(没有权限问题)。我可能以某种方式配置了错误的路由。有任何想法吗?
1 回答

PIPIONE
TA贡献1829条经验 获得超9个赞
FileServer 正在尝试将 /node_modules/... 等路径路由到文件“client/node_modules/node_modules/...”
所以使用StripPrefix,例如:
http.Handle("/node_modules", http.StripPrefix("/node_modules", http.FileServer(http.Dir(("client/node_modules")))))
在此处查看另一个答案。
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报
0/150
提交
取消