【问题标题】:http.FileServer is sending "404 page not found"http.FileServer 正在发送“404 页面未找到”
【发布时间】:2023-03-05 05:56:01
【问题描述】:

我正在尝试通过 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

【问题讨论】:

标签: go


【解决方案1】:

目录/assets 是否存在?请注意,/assets 是绝对路径,因此它必须位于文件系统的根目录。如果你想在你正在执行你的程序的工作目录中找到一些东西,你应该使用./assets

【讨论】:

  • 这行得通!太感谢了。这个问题困扰了我好几个星期
  • 我改成“./assets”还是404
【解决方案2】:

如果你使用相对路径,你可以检查你的路径是什么。

import (
"fmt"
"os"
)

dir, _ := os.Getwd()
fmt.Println("current path :" + dir)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    相关资源
    最近更新 更多