英文:
css files are not loaded if you load from"/"
问题
我找到了一个解决方案,但只有在指定路径的情况下才有效。
fileServer := http.FileServer(http.Dir("./html"))
http.Handle("/html/", http.StripPrefix("/html/", fileServer))
当我尝试从"/"路径进行访问时,没有任何内容显示,包括以下代码:
http.Handle("/", http.FileServer(http.Dir("./html/")))
英文:
I found a solution, but it only works if the path is specified
fileServer := http.FileServer(http.Dir("./html"))
http.Handle("/html/", http.StripPrefix("/html/", fileServer))
When I try to do it from "/" nothing comes out
including
http.handle("/", http.FileServer(http.Dir("./html/")))
答案1
得分: 1
请看下面的程序,它将测试两种情况。
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"text/tabwriter"
"time"
)
func main() {
srv1 := &http.Server{
Addr: "localhost:9090",
}
go func() {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./html"))
mux.Handle("/", fileServer)
srv1.Handler = mux
srv1.ListenAndServe()
}()
srv2 := &http.Server{
Addr: "localhost:9091",
}
go func() {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./html"))
mux.Handle("/html/", http.StripPrefix("/html/", fileServer))
srv2.Handler = mux
srv2.ListenAndServe()
}()
srvs := []*http.Server{srv1, srv2}
urls := []string{
"http://%v/index.css",
"http://%v/html/index.css",
}
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
defer fmt.Println()
defer w.Flush()
fmt.Fprintf(w, "\n %s\t%s\t", "URL", "Response")
fmt.Fprintf(w, "\n %s\t%s\t", "----", "----")
for _, srv := range srvs {
for _, f := range urls {
<-time.After(time.Millisecond * 200)
u := fmt.Sprintf(f, srv.Addr)
res, err := http.Get(u)
if err != nil {
continue
}
var out bytes.Buffer
io.Copy(&out, res.Body)
res.Body.Close()
fmt.Fprintf(w, "\n %s\t%s\t", u, fmt.Sprintf("%q", out.String()))
}
}
}
输出结果为:
$ go run .
URL Response
---- ----
http://localhost:9090/index.css "OK\n"
http://localhost:9090/html/index.css "404 page not found\n"
http://localhost:9091/index.css "404 page not found\n"
http://localhost:9091/html/index.css "OK\n"
该程序通过手动创建两个监听不同本地地址的HTTP服务器来实现。每个服务器都有不同的mux设置。然后使用多个不同的URL对两个服务器进行GET请求。在继续之前,会多次重试获取,直到没有错误发生,以解决服务器启动的不同步问题。输出结果使用TabWriter进行格式化。
英文:
See for yourself, below program will test both cases.
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"text/tabwriter"
"time"
)
func main() {
srv1 := &http.Server{
Addr: "localhost:9090",
}
go func() {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./html"))
mux.Handle("/", fileServer)
srv1.Handler = mux
srv1.ListenAndServe()
}()
srv2 := &http.Server{
Addr: "localhost:9091",
}
go func() {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir("./html"))
mux.Handle("/html/", http.StripPrefix("/html/", fileServer))
srv2.Handler = mux
srv2.ListenAndServe()
}()
srvs := []*http.Server{srv1, srv2}
urls := []string{
"http://%v/index.css",
"http://%v/html/index.css",
}
// u := fmt.Sprintf("http://%v/html/index.css", srv.Addr)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, '\t', 0)
defer fmt.Println()
defer w.Flush()
fmt.Fprintf(w, "\n %s\t%s\t", "URL", "Response")
fmt.Fprintf(w, "\n %s\t%s\t", "----", "----")
for _, srv := range srvs {
for _, f := range urls {
<-time.After(time.Millisecond * 200)
u := fmt.Sprintf(f, srv.Addr)
res, err := http.Get(u)
if err != nil {
continue
}
var out bytes.Buffer
io.Copy(&out, res.Body)
res.Body.Close()
fmt.Fprintf(w, "\n %s\t%s\t", u, fmt.Sprintf("%q", out.String()))
}
}
}
It outputs
$ go run .
URL Response
---- ----
http://localhost:9090/index.css "OK\n"
http://localhost:9090/html/index.css "404 page not found\n"
http://localhost:9091/index.css "404 page not found\n"
http://localhost:9091/html/index.css "OK\n"
It works by manually creating two HTTP servers that listens on distinct local addresses. Each server gets a different mux setup. Then it GETs both server with multiple different URLs. The fetch is retried multiple times until no error occurs to account for the un-syncrhonized servers startup before proceeding. The output is formatted with the help of a TabWriter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论