🎭
Grouping

Paths

Like Routing, groups can also have paths that belong to a cluster.
1
func main() {
2
app := fiber.New()
3
​
4
api := app.Group("/api", middleware) // /api
5
​
6
v1 := api.Group("/v1", middleware) // /api/v1
7
v1.Get("/list", handler) // /api/v1/list
8
v1.Get("/user", handler) // /api/v1/user
9
​
10
v2 := api.Group("/v2", middleware) // /api/v2
11
v2.Get("/list", handler) // /api/v2/list
12
v2.Get("/user", handler) // /api/v2/user
13
​
14
log.Fatal(app.Listen(":3000"))
15
}
Copied!
A Group of paths can have an optional handler.
1
func main() {
2
app := fiber.New()
3
​
4
api := app.Group("/api") // /api
5
​
6
v1 := api.Group("/v1") // /api/v1
7
v1.Get("/list", handler) // /api/v1/list
8
v1.Get("/user", handler) // /api/v1/user
9
​
10
v2 := api.Group("/v2") // /api/v2
11
v2.Get("/list", handler) // /api/v2/list
12
v2.Get("/user", handler) // /api/v2/user
13
​
14
log.Fatal(app.Listen(":3000"))
15
}
Copied!
Running /api, /v1 or /v2 will result in 404 error, make sure you have the errors set.

Group Handlers

Group handlers can also be used as a routing path but they must have Next added to them so that the flow can continue.
1
func main() {
2
app := fiber.New()
3
​
4
handler := func(c *fiber.Ctx) error {
5
return c.SendStatus(fiber.StatusOK)
6
}
7
api := app.Group("/api") // /api
8
​
9
v1 := api.Group("/v1", func(c *fiber.Ctx) error { // middleware for /api/v1
10
c.Set("Version", "v1")
11
return c.Next()
12
})
13
v1.Get("/list", handler) // /api/v1/list
14
v1.Get("/user", handler) // /api/v1/user
15
​
16
log.Fatal(app.Listen(":3000"))
17
}
Copied!
Last modified 7mo ago
Copy link