我试图通过http.FileServer提供静态文件,但是它从来没有发送回我想要的目录。代码如下所示:
func main() {
fmt.Println("Serving Files")
http.HandleFunc("/", homeFunc)
http.HandleFunc("/search", searchFunc)
http.Handle("/tmp/",
http.StripPrefix("/tmp/", http.FileServer(http.Dir("/assets"))))
http.ListenAndServe(":8080", nil)
}访问mywebsite.com/tmp/时,会出现"404页未找到“的文本。如果我错过了一些东西,我会非常感激的!
编辑:这是文件体系结构:
main folder
|
|-/Assets
|--(assets)
|
|-main.go发布于 2017-06-27 14:52:41
目录/assets存在吗?请注意,/assets是一个绝对路径,因此它必须位于文件系统的根目录。如果您想要执行程序的工作目录中的某些内容,则应该使用./assets。
发布于 2020-01-23 09:35:59
如果使用相对路径,则可以检查路径是什么。
import (
"fmt"
"os"
)
dir, _ := os.Getwd()
fmt.Println("current path :" + dir)https://stackoverflow.com/questions/44782680
复制相似问题