1

我有以下代码:

r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("./frontend/build/")))
r.Handle("/static", http.FileServer(http.Dir("./frontend/build/static/")))
r.PathPrefix("/api").Handler(auth)

/api应该是安全的。如果用户点击/,我希望他们查看目录index.html中的。PROJECTDIR/frontend

前端目录看起来像

frontend
    /build
        index.html
        /static
            /js
            /css
            /media

index.html 从 加载所有内容/static。无论我如何配置它,当我访问时localhost:3000,我都可以得到index.html但下面的所有内容/static都是 404 的。

我如何错误地配置它?

4

1 回答 1

4

假设您想在端点 /static 上提供目录“静态”的全部内容,并且您正在运行 bsd/linux 机器,则以下语法应该有效:

http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
于 2017-04-26T09:54:12.530 回答