github.com/gofiber/fiber/v2@v2.47.0/docs/guide/grouping.md (about) 1 --- 2 id: grouping 3 title: 🎠Grouping 4 sidebar_position: 2 5 --- 6 7 :::info 8 In general, the Group functionality in Fiber behaves similarly to ExpressJS. Groups are declared virtually and all routes declared within the group are flattened into a single list with a prefix, which is then checked by the framework in the order it was declared. This means that the behavior of Group in Fiber is identical to that of ExpressJS. 9 ::: 10 11 ## Paths 12 13 Like **Routing**, groups can also have paths that belong to a cluster. 14 15 ```go 16 func main() { 17 app := fiber.New() 18 19 api := app.Group("/api", middleware) // /api 20 21 v1 := api.Group("/v1", middleware) // /api/v1 22 v1.Get("/list", handler) // /api/v1/list 23 v1.Get("/user", handler) // /api/v1/user 24 25 v2 := api.Group("/v2", middleware) // /api/v2 26 v2.Get("/list", handler) // /api/v2/list 27 v2.Get("/user", handler) // /api/v2/user 28 29 log.Fatal(app.Listen(":3000")) 30 } 31 ``` 32 33 A **Group** of paths can have an optional handler. 34 35 ```go 36 func main() { 37 app := fiber.New() 38 39 api := app.Group("/api") // /api 40 41 v1 := api.Group("/v1") // /api/v1 42 v1.Get("/list", handler) // /api/v1/list 43 v1.Get("/user", handler) // /api/v1/user 44 45 v2 := api.Group("/v2") // /api/v2 46 v2.Get("/list", handler) // /api/v2/list 47 v2.Get("/user", handler) // /api/v2/user 48 49 log.Fatal(app.Listen(":3000")) 50 } 51 ``` 52 53 :::caution 54 Running **/api**, **/v1** or **/v2** will result in **404** error, make sure you have the errors set. 55 ::: 56 57 ## Group Handlers 58 59 Group handlers can also be used as a routing path but they must have **Next** added to them so that the flow can continue. 60 61 ```go 62 func main() { 63 app := fiber.New() 64 65 handler := func(c *fiber.Ctx) error { 66 return c.SendStatus(fiber.StatusOK) 67 } 68 api := app.Group("/api") // /api 69 70 v1 := api.Group("/v1", func(c *fiber.Ctx) error { // middleware for /api/v1 71 c.Set("Version", "v1") 72 return c.Next() 73 }) 74 v1.Get("/list", handler) // /api/v1/list 75 v1.Get("/user", handler) // /api/v1/user 76 77 log.Fatal(app.Listen(":3000")) 78 } 79 ```