description: >- Routing refers to how an application’s endpoints (URIs) respond to client

requests.

🔌 Routing

Paths

Route paths, combined with a request method, define the endpoints at which requests can be made. Route paths can be strings or string patterns.

Examples of route paths based on strings

  1. // This route path will match requests to the root route, "/":
  2. app.Get("/", func(c *fiber.Ctx) error {
  3. return c.SendString("root")
  4. })
  5. // This route path will match requests to "/about":
  6. app.Get("/about", func(c *fiber.Ctx) error {
  7. return c.SendString("about")
  8. })
  9. // This route path will match requests to "/random.txt":
  10. app.Get("/random.txt", func(c *fiber.Ctx) error {
  11. return c.SendString("random.txt")
  12. })

Parameters

Route parameters are dynamic elements in the route, which are named or not named segments. This segments that are used to capture the values specified at their position in the URL. The obtained values can be retrieved using the Params function, with the name of the route parameter specified in the path as their respective keys or for unnamed parameters the character(*, +) and the counter of this.

The characters :, +, and * are characters that introduce a parameter.

Greedy parameters are indicated by wildcard(*) or plus(+) signs.

The routing also offers the possibility to use optional parameters, for the named parameters these are marked with a final “?”, unlike the plus sign which is not optional, you can use the wildcard character for a parameter range which is optional and greedy.

Example of define routes with route parameters

  1. // Parameters
  2. app.Get("/user/:name/books/:title", func(c *fiber.Ctx) error {
  3. fmt.Fprintf(c, "%s\n", c.Params("name"))
  4. fmt.Fprintf(c, "%s\n", c.Params("title"))
  5. return nil
  6. })
  7. // Plus - greedy - not optional
  8. app.Get("/user/+", func(c *fiber.Ctx) error {
  9. return c.SendString(c.Params("+"))
  10. })
  11. // Optional parameter
  12. app.Get("/user/:name?", func(c *fiber.Ctx) error {
  13. return c.SendString(c.Params("name"))
  14. })
  15. // Wildcard - greedy - optional
  16. app.Get("/user/*", func(c *fiber.Ctx) error {
  17. return c.SendString(c.Params("*"))
  18. })
  19. // This route path will match requests to "/v1/some/resource/name:customVerb", since the parameter character is escaped
  20. app.Get("/v1/some/resource/name\\:customVerb", func(c *fiber.Ctx) error {
  21. return c.SendString("Hello, Community")
  22. })

Since the hyphen (-) and the dot (.) are interpreted literally, they can be used along with route parameters for useful purposes.

All special parameter characters can also be escaped with "\\" and lose their value, so you can use them in the route if you want, like in the custom methods of the google api design guide.

  1. // http://localhost:3000/plantae/prunus.persica
  2. app.Get("/plantae/:genus.:species", func(c *fiber.Ctx) error {
  3. fmt.Fprintf(c, "%s.%s\n", c.Params("genus"), c.Params("species"))
  4. return nil // prunus.persica
  5. })
  1. // http://localhost:3000/flights/LAX-SFO
  2. app.Get("/flights/:from-:to", func(c *fiber.Ctx) error {
  3. fmt.Fprintf(c, "%s-%s\n", c.Params("from"), c.Params("to"))
  4. return nil // LAX-SFO
  5. })

Our intelligent router recognizes that the introductory parameter characters should be part of the request route in this case and can process them as such.

  1. // http://localhost:3000/shop/product/color:blue/size:xs
  2. app.Get("/shop/product/color::color/size::size", func(c *fiber.Ctx) error {
  3. fmt.Fprintf(c, "%s:%s\n", c.Params("color"), c.Params("size"))
  4. return nil // blue:xs
  5. })

In addition, several parameters in a row and several unnamed parameter characters in the route, such as the wildcard or plus character, are possible, which greatly expands the possibilities of the router for the user.

  1. // GET /@v1
  2. // Params: "sign" -> "@", "param" -> "v1"
  3. app.Get("/:sign:param", handler)
  4. // GET /api-v1
  5. // Params: "name" -> "v1"
  6. app.Get("/api-:name", handler)
  7. // GET /customer/v1/cart/proxy
  8. // Params: "*1" -> "customer/", "*2" -> "/cart"
  9. app.Get("/*v1*/proxy", handler)
  10. // GET /v1/brand/4/shop/blue/xs
  11. // Params: "*1" -> "brand/4", "*2" -> "blue/xs"
  12. app.Get("/v1/*/shop/*", handler)

We have adapted the routing strongly to the express routing, but currently without the possibility of the regular expressions, because they are quite slow. The possibilities can be tested with version 0.1.7 (express 4) in the online Express route tester.

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) error {
  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. // Go to next middleware:
  10. return c.Next()
  11. })
  12. app.Get("/", func(c *fiber.Ctx) error {
  13. return c.SendString("Hello, World!")
  14. })

Use method path is a mount, or prefix path, and limits middleware to only apply to any paths requested that begin with it.

Grouping

If you have many endpoints, you can organize your routes using Group.

  1. func main() {
  2. app := fiber.New()
  3. api := app.Group("/api", middleware) // /api
  4. v1 := api.Group("/v1", middleware) // /api/v1
  5. v1.Get("/list", handler) // /api/v1/list
  6. v1.Get("/user", handler) // /api/v1/user
  7. v2 := api.Group("/v2", middleware) // /api/v2
  8. v2.Get("/list", handler) // /api/v2/list
  9. v2.Get("/user", handler) // /api/v2/user
  10. log.Fatal(app.Listen(":3000"))
  11. }