go-web icon indicating copy to clipboard operation
go-web copied to clipboard

apart code of chapter 6 not work

Open johnsmeeth opened this issue 7 years ago • 0 comments

Hi @shijuvar I've read this example in chapter 6

router := mux.NewRouter()
adminRoutes := mux.NewRouter()
// add admin routes here
// Create a new negroni for the admin middleware
router.Handle("/admin", negroni.New(
Middleware1,
Middleware2,
negroni.Wrap(adminRoutes),
))

But this piece of code

router.Handle("/admin", negroni.New(
Middleware1,
Middleware2,
negroni.Wrap(adminRoutes),
))

seem not work. When visit url http://localhost:8080/admin or http://localhost:8080/admin/dashboard both seem the handlers for /admin not executed.

This is my handlers for /admin route

	// adminRouter routes
	adminRoutes.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("/admin")
		fmt.Fprintf(w, "/admin")
	}))
	
	adminRoutes.Handle("/dashboard", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("/admin/dashboard")
		fmt.Fprintf(w, "dashboard page")
	}))

What wrong with my code? Your book so great. Thanks

johnsmeeth avatar Feb 01 '18 04:02 johnsmeeth