Skip to content

Middleware for matched routes only #2575

Answered by aldas
cemremengu asked this question in Q&A
Discussion options

You must be logged in to vote

When router did not find route the path will be empty. You can check this with c.Path() == ""

	e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
		Skipper: func(c echo.Context) bool {
			return c.Path() == "" // logger middleware will be skipped when no route matched
		},
	}))

or you could add middleware directly to routes and avoid using e.Use

	e.GET("/hello", func(c echo.Context) error {
		return c.String(200, "Hello, World!")
	}, middleware.Logger())

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by cemremengu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants