Middleware for matched routes only #2575
              
                
                  
                  
                    Answered
                  
                  by
                    aldas
                  
              
          
                  
                    
                      cemremengu
                    
                  
                
                  asked this question in
                Q&A
              
            -
| How do you run middleware(s) only for matched routes i.e non 404 ? | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            aldas
          
      
      
        Jan 13, 2024 
      
    
    Replies: 1 comment
-
| When router did not find route the path will be empty. You can check this with  	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.GET("/hello", func(c echo.Context) error {
		return c.String(200, "Hello, World!")
	}, middleware.Logger()) | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        cemremengu
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
When router did not find route the path will be empty. You can check this with
c.Path() == ""or you could add middleware directly to routes and avoid using
e.Use