如果你从”/”加载,CSS文件将无法加载。

huangapple go评论84阅读模式
英文:

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 (
&quot;bytes&quot;
&quot;fmt&quot;
&quot;io&quot;
&quot;net/http&quot;
&quot;os&quot;
&quot;text/tabwriter&quot;
&quot;time&quot;
)
func main() {
srv1 := &amp;http.Server{
Addr: &quot;localhost:9090&quot;,
}
go func() {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir(&quot;./html&quot;))
mux.Handle(&quot;/&quot;, fileServer)
srv1.Handler = mux
srv1.ListenAndServe()
}()
srv2 := &amp;http.Server{
Addr: &quot;localhost:9091&quot;,
}
go func() {
mux := http.NewServeMux()
fileServer := http.FileServer(http.Dir(&quot;./html&quot;))
mux.Handle(&quot;/html/&quot;, http.StripPrefix(&quot;/html/&quot;, fileServer))
srv2.Handler = mux
srv2.ListenAndServe()
}()
srvs := []*http.Server{srv1, srv2}
urls := []string{
&quot;http://%v/index.css&quot;,
&quot;http://%v/html/index.css&quot;,
}
// u := fmt.Sprintf(&quot;http://%v/html/index.css&quot;, srv.Addr)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 8, 8, 0, &#39;\t&#39;, 0)
defer fmt.Println()
defer w.Flush()
fmt.Fprintf(w, &quot;\n %s\t%s\t&quot;, &quot;URL&quot;, &quot;Response&quot;)
fmt.Fprintf(w, &quot;\n %s\t%s\t&quot;, &quot;----&quot;, &quot;----&quot;)
for _, srv := range srvs {
for _, f := range urls {
&lt;-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(&amp;out, res.Body)
res.Body.Close()
fmt.Fprintf(w, &quot;\n %s\t%s\t&quot;, u, fmt.Sprintf(&quot;%q&quot;, out.String()))
}
}
}

It outputs

$ go run .
URL					                Response		
----					                ----			
http://localhost:9090/index.css	    &quot;OK\n&quot;			
http://localhost:9090/html/index.css	&quot;404 page not found\n&quot;	
http://localhost:9091/index.css	    &quot;404 page not found\n&quot;	
http://localhost:9091/html/index.css	&quot;OK\n&quot;

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.

huangapple
  • 本文由 发表于 2021年9月29日 03:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/69367328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定