Sign in
Log inSign up

How would you prefer to use middleware in your web app?

Eslam Mostafa's photo
Eslam Mostafa
·Apr 1, 2017

I've seen routers that allows you to use middleware like this:

router.Use(yourMiddleware)

and i've been thinking that there is another approach and i want to know if it's useful or not, I am practicing on coding a mux in go Garson and it occurred to me i could implement middleware usage like this

router := garson.New() 
// the next line registers two middlewares to be executed before each request 
router.Before(garson.LoggerMiddleware, garson.OnlyJSONMiddleware) 
// the next line registers one middleware to be executed after each request 
router.After(garson.SomeOtherMiddlware) 
// the next line registers one middleware to be executed only before this route 
router.Post("/api/comments").Before(AuthMiddleware)

what do you guys think ? in terms of real projects usage.