Middleware

Functions, that are designed to make changes to the request or response, are called middleware functions. The Next is a Fiber router function, when called, executes the next function that matches the current route.

Example of a middleware function

  1. app.Use(func(c *fiber.Ctx) {
  2. // Set some security headers:
  3. c.Set("X-XSS-Protection", "1; mode=block")
  4. c.Set("X-Content-Type-Options", "nosniff")
  5. c.Set("X-Download-Options", "noopen")
  6. c.Set("Strict-Transport-Security", "max-age=5184000")
  7. c.Set("X-Frame-Options", "SAMEORIGIN")
  8. c.Set("X-DNS-Prefetch-Control", "off")
  9.  
  10. // Go to next middleware:
  11. c.Next()
  12. })
  13.  
  14. app.Get("/", func(c *fiber.Ctx) {
  15. c.Send("Hello, World!")
  16. })

Use method path is a mount or prefix path and limits middleware to only apply to any paths requested that begin with it. This means you cannot use :params on the Use method.