运行一个简单的服务器,但计数器似乎每次增加3,为什么?

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

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/countcounter会显示为3,然后我访问localhost:8000,再次访问localhost:8000/countcounter现在会显示为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

huangapple
  • 本文由 发表于 2016年3月15日 15:59:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/36005489.html
匿名

发表评论

匿名网友

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

确定