chi icon indicating copy to clipboard operation
chi copied to clipboard

chi mux mount http.ServMux not working,got 404

Open it512 opened this issue 5 months ago • 2 comments

package main

import (
	"fmt"
	"net/http"

	"github.com/go-chi/chi/v5"
)

func main() {
	root := chi.NewMux()
	mux := http.NewServeMux()
	mux.HandleFunc("GET /world", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "hello world")
	})
	root.Mount("/hello", mux)
	http.ListenAndServe(":10011", root)
}

got a 404

it512 avatar Jul 13 '25 06:07 it512

@it512

Since inner mechanism to find route in net/http and chi are different, it is hard to mix http and chi with Mount method.

  • for net/http : using key-value mapping to find path https://github.com/golang/go/blob/ce3f3e2ae73dcc0c270bc73a59ea5e7be6cf6a8d/src/net/http/mapping.go#L43

  • for chi : using radix tree to find path https://github.com/go-chi/chi/blob/23c395f8524a30334126ca16fb4d37b88745b9b9/tree.go#L399

Therefore you can change "GET /world" to "GET /hello/world" and you can get the right behavior. (chi's Mount do not influence over net/http)

Do you have any prerequirements to mix net/http and chi or you can just bear to use chi only ?

YmBIgo avatar Aug 12 '25 02:08 YmBIgo

thanks a lot!

I am using chi, but 3rd lib provide std mux

it512 avatar Aug 25 '25 00:08 it512