如果从“/”加载,则不会加载 css 文件

我找到了一个解决方案,但它只有在指定路径时才有效


fileServer := http.FileServer(http.Dir("./html"))

http.Handle("/html/", http.StripPrefix("/html/", fileServer))

当我尝试从“/”中执行此操作时,没有任何内容,包括


http.handle("/", http.FileServer(http.Dir("./html/")))


繁星点点滴滴
浏览 77回答 1
1回答

拉莫斯之舞

请您亲自查看,下面的程序将测试这两种情况。package mainimport (&nbsp; &nbsp; "bytes"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "os"&nbsp; &nbsp; "text/tabwriter"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; srv1 := &http.Server{&nbsp; &nbsp; &nbsp; &nbsp; Addr: "localhost:9090",&nbsp; &nbsp; }&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; mux := http.NewServeMux()&nbsp; &nbsp; &nbsp; &nbsp; fileServer := http.FileServer(http.Dir("./html"))&nbsp; &nbsp; &nbsp; &nbsp; mux.Handle("/", fileServer)&nbsp; &nbsp; &nbsp; &nbsp; srv1.Handler = mux&nbsp; &nbsp; &nbsp; &nbsp; srv1.ListenAndServe()&nbsp; &nbsp; }()&nbsp; &nbsp; srv2 := &http.Server{&nbsp; &nbsp; &nbsp; &nbsp; Addr: "localhost:9091",&nbsp; &nbsp; }&nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; mux := http.NewServeMux()&nbsp; &nbsp; &nbsp; &nbsp; fileServer := http.FileServer(http.Dir("./html"))&nbsp; &nbsp; &nbsp; &nbsp; mux.Handle("/html/", http.StripPrefix("/html/", fileServer))&nbsp; &nbsp; &nbsp; &nbsp; srv2.Handler = mux&nbsp; &nbsp; &nbsp; &nbsp; srv2.ListenAndServe()&nbsp; &nbsp; }()&nbsp; &nbsp; srvs := []*http.Server{srv1, srv2}&nbsp; &nbsp; urls := []string{&nbsp; &nbsp; &nbsp; &nbsp; "http://%v/index.css",&nbsp; &nbsp; &nbsp; &nbsp; "http://%v/html/index.css",&nbsp; &nbsp; }&nbsp; &nbsp; // u := fmt.Sprintf("http://%v/html/index.css", srv.Addr)&nbsp; &nbsp; w := new(tabwriter.Writer)&nbsp; &nbsp; w.Init(os.Stdout, 8, 8, 0, '\t', 0)&nbsp; &nbsp; defer fmt.Println()&nbsp; &nbsp; defer w.Flush()&nbsp; &nbsp; fmt.Fprintf(w, "\n %s\t%s\t", "URL", "Response")&nbsp; &nbsp; fmt.Fprintf(w, "\n %s\t%s\t", "----", "----")&nbsp; &nbsp; for _, srv := range srvs {&nbsp; &nbsp; &nbsp; &nbsp; for _, f := range urls {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <-time.After(time.Millisecond * 200)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; u := fmt.Sprintf(f, srv.Addr)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res, err := http.Get(u)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var out bytes.Buffer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; io.Copy(&out, res.Body)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.Body.Close()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintf(w, "\n %s\t%s\t", u, fmt.Sprintf("%q", out.String()))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}它输出$ go run .&nbsp;URL&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Response&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;----&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;----&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;http://localhost:9090/index.css&nbsp; &nbsp; &nbsp; &nbsp; "OK\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;http://localhost:9090/html/index.css&nbsp; &nbsp;"404 page not found\n"&nbsp;&nbsp;&nbsp;http://localhost:9091/index.css&nbsp; &nbsp; &nbsp; &nbsp; "404 page not found\n"&nbsp;&nbsp;&nbsp;http://localhost:9091/html/index.css&nbsp; &nbsp;"OK\n"它的工作原理是手动创建两个侦听不同本地地址的&nbsp;HTTP 服务器。每个服务器都有不同的多路复用器设置。然后它获取具有多个不同URL的两个服务器。在继续之前,将重试多次抓取,直到没有发生错误来解释未同步的服务器启动。输出是在&nbsp;TabWriter&nbsp;的帮助下格式化的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go