英文:
Running a simple server, but the counter seems to go up by 3, why?
问题
我有一个小服务器。意图是,如果我访问localhost:8000/*,它应该将counter
增加1,如果我访问localhost:8000/count,它应该显示当前的counter
数。
一个奇怪的事情发生了,似乎每次我访问localhost:8000
,计数器都会增加3。所以我会去localhost:8000/count
,counter
会显示为3,然后我访问localhost:8000
,再次访问localhost:8000/count
,counter
现在会显示为6。为什么会这样?net/http
有什么奇怪的地方吗?
另外,为什么当我刷新localhost:8000/count
时,计数会逐渐增加1?counter
函数没有增加count
,但count
仍然增加了-为什么会这样?handler
也会添加到localhost:8000/count
路由吗?
package main
import (
"fmt"
"log"
"net/http"
"sync"
)
var mu sync.Mutex
var count int
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/count", counter)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
// handler echoes the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
count++
mu.Unlock()
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
}
// counter echoes the number of calls so far.
func counter(w http.ResponseWriter, r *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count %d\n", count)
mu.Unlock()
}
英文:
I have this little server here. The intent is that if I visit localhost:8000/* it should increase counter
by 1, and if I go to localhost:8000/count, it should show me the current number of counter
.
A weird thing happening is that it seems like every time I visit localhost:8000
, the counter goes up by 3. So I'd go to localhost:8000/count
and the counter
would be at 3, and then I visit localhost:8000
, and then localhost:8000/count
again, counter
would now be at 6. Why does that happen? Is there something weird with net/http
?
Also, why is it that when I refresh localhost:8000/count
, the count goes up 1 by 1? The counter
func doesn't increment count
, yet count
still goes up - why is that? Does handler
get added to the localhost:8000/count
route as well?
package main
import (
"fmt"
"log"
"net/http"
"sync"
)
var mu sync.Mutex
var count int
func main() {
http.HandleFunc("/", handler)
http.HandleFunc("/count", counter)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
}
// handler echoes the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
count++
mu.Unlock()
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
}
// counter echoes the number of calls so far.
func counter(w http.ResponseWriter, r *http.Request) {
mu.Lock()
fmt.Fprintf(w, "Count %d\n", count)
mu.Unlock()
}
答案1
得分: 3
额外的请求是你的浏览器尝试访问 /favicon.ico。
例如,如果你在处理函数中将 http.Request 打印到标准输出,你就可以看到这一点。
英文:
The extra requests are your browser trying to access /favicon.ico
You can see this, for example, if you print the http.Request to stdout in your handler funcs.
答案2
得分: 1
问题在于您的处理程序正在提供额外的静态内容,例如favicon
等。如果记录请求,您会发现您的浏览器可能还请求了/favicon.ico,该请求也被传递给了同一个处理程序。
一种提供静态内容的方法是创建自己的ServeMux:
package main
import (
"fmt"
"net/http"
"strings"
)
var chttp = http.NewServeMux()
func main() {
chttp.Handle("/", http.FileServer(http.Dir("./")))
http.HandleFunc("/", HomeHandler) // homepage
http.ListenAndServe(":8080", nil)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
if (strings.Contains(r.URL.Path, ".")) {
chttp.ServeHTTP(w, r)
} else {
fmt.Fprintf(w, "HomeHandler")
}
}
参考链接:https://stackoverflow.com/questions/14086063/serve-homepage-and-static-content-from-root
英文:
The issue is that your handler is serving extra static content like favicon
and such. If you log the request, you'll see your browser is probably also requesting
/favicon.ico, which is being passed to that same handler.
To serve static content one way is to create your own ServeMux:
package main
import (
"fmt"
"net/http"
"strings"
)
var chttp = http.NewServeMux()
func main() {
chttp.Handle("/", http.FileServer(http.Dir("./")))
http.HandleFunc("/", HomeHandler) // homepage
http.ListenAndServe(":8080", nil)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
if (strings.Contains(r.URL.Path, ".")) {
chttp.ServeHTTP(w, r)
} else {
fmt.Fprintf(w, "HomeHandler")
}
}
https://stackoverflow.com/questions/14086063/serve-homepage-and-static-content-from-root
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论