dongye7231 2015-03-24 07:06
浏览 38
已采纳

没有引用CSS文件

I have this code as my myApp.go:

package fastaticapp

import (
 "html/template"
 "log"
 "net/http"
)

func init() {
http.HandleFunc("/", rootHandler)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("css"))))

if r.URL.Path != "/" {
    errorHandler(w, r, http.StatusNotFound, "")
    return
}

page := template.Must(template.ParseFiles(
    "static/_base.html",
    "static/index.html",
))

if err := page.Execute(w, nil); err != nil {
    errorHandler(w, r, http.StatusInternalServerError, err.Error())
    return
}

}

And the direcotry Layout looks like this:

webapp/
  myApp/
    server.go
  static/
    index.html
    _base.html
    img/
    css/
     main.css

I am using template package with Must function. running the dev server, the HTML page gets rendered by the browser and using developer tools I can see the html contents. The problem is main.css file within the css folder does not appear to be properly handled by the server. Any thought on this would be highly appreciated.

  • 写回答

1条回答 默认 最新

  • dpl9717 2015-03-24 08:10
    关注

    Use

    http.Dir("static/css")
    

    instead of

    http.Dir("css")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?