1

我有这个目录结构,我正在使用 Gorilla mux:

目录结构

twitter
    layout
        stylesheets
            log.css
        log.html
    twitter.go

按照这里的建议:http: //www.shakedos.com/2014/Feb/08/serving-static-files-with-go.html我这样做了:

var router = mux.NewRouter()

func ServeStatic(router *mux.Router, staticDirectory string) {
    staticPaths := map[string]string{
        "styles": staticDirectory + "stylesheets",
        }
    for pathName, pathValue := range staticPaths {
        pathPrefix := "/" + pathName + "/"
        router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
        http.FileServer(http.Dir(pathValue))))
    }
}

var staticDirectory = "/layout/"

func main() {
    (//other code)
    ServeStatic(router, staticDirectory)
}

我仍然无法链接 CSS 文件。我究竟做错了什么?

4

2 回答 2

6

解决。

我在 func main() 中添加了这个

router.PathPrefix("/").Handler(http.FileServer(http.Dir("./layout/")))
于 2014-08-19T02:55:09.907 回答
0

您可以以更简单的方式执行此操作,而无需在 main() 中添加额外的行:

在 ServeStatic 中:在 pathValue 之前添加:“.”+

router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix,
            http.FileServer(http.Dir("."/pathValue))))
于 2016-02-29T12:35:58.047 回答