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
- app.Use(func(c *fiber.Ctx) {
- // Set some security headers:
- c.Set("X-XSS-Protection", "1; mode=block")
- c.Set("X-Content-Type-Options", "nosniff")
- c.Set("X-Download-Options", "noopen")
- c.Set("Strict-Transport-Security", "max-age=5184000")
- c.Set("X-Frame-Options", "SAMEORIGIN")
- c.Set("X-DNS-Prefetch-Control", "off")
- // Go to next middleware:
- c.Next()
- })
- app.Get("/", func(c *fiber.Ctx) {
- c.Send("Hello, World!")
- })
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.