Skip to content

Commit 32cbb28

Browse files
authored
Merge pull request #4311 from gofiber/docs/routing-guide-restructure
docs: restructure routing guide and split handler partials
2 parents 86397b0 + 47ca494 commit 32cbb28

6 files changed

Lines changed: 488 additions & 266 deletions

File tree

docs/api/app.md

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,29 @@ id: app
33
title: 🚀 App
44
description: The `App` type represents your Fiber application.
55
sidebar_position: 2
6+
toc_max_heading_level: 4
67
---
78

89
import Reference from '@site/src/components/reference';
910

10-
## Helpers
11-
12-
### GetString
13-
14-
Returns `s` unchanged when [`Immutable`](./fiber.md#immutable) is disabled or `s` resides in read-only memory. Otherwise, it returns a detached copy using `strings.Clone`.
15-
16-
```go title="Signature"
17-
func (app *App) GetString(s string) string
18-
```
19-
20-
### GetBytes
21-
22-
Returns `b` unchanged when [`Immutable`](./fiber.md#immutable) is disabled or `b` resides in read-only memory. Otherwise, it returns a detached copy.
23-
24-
```go title="Signature"
25-
func (app *App) GetBytes(b []byte) []byte
26-
```
27-
28-
### ReloadViews
29-
30-
Reloads the configured view engine on demand by calling its `Load` method. Use this helper in development workflows (e.g., file watchers or debug-only routes) to pick up template changes without restarting the server. Returns an error if no view engine is configured or reloading fails.
31-
32-
```go title="Signature"
33-
func (app *App) ReloadViews() error
34-
```
35-
36-
```go title="Example"
37-
app := fiber.New(fiber.Config{Views: engine})
38-
39-
app.Get("/dev/reload", func(c fiber.Ctx) error {
40-
if err := app.ReloadViews(); err != nil {
41-
return err
42-
}
43-
return c.SendString("Templates reloaded")
44-
})
45-
```
46-
4711
## Routing
4812

4913
import RoutingHandler from './../partials/routing/handler.md';
14+
import RoutingUse from './../partials/routing/use.md';
5015

5116
### Route Handlers
5217

5318
<RoutingHandler />
5419

20+
Beyond the native `func(fiber.Ctx)` forms, Fiber also adapts Express-style, `net/http`, and `fasthttp` handlers. See [Handler types](../guide/routing.md#handler-types) in the routing guide for the full list of supported shapes.
21+
22+
### Use
23+
24+
<RoutingUse />
25+
5526
### Mounting
5627

57-
Mount another Fiber instance with [`app.Use`](./app.md#use), similar to Express's [`router.use`](https://expressjs.com/en/api.html#router.use).
28+
Mount another Fiber instance with [`app.Use`](#use), similar to Express's [`router.use`](https://expressjs.com/en/api.html#router.use).
5829

5930
```go title="Example"
6031
package main
@@ -80,17 +51,9 @@ func main() {
8051
}
8152
```
8253

83-
### State / SharedState
84-
85-
`State()` returns in-process state (local to the current process).
86-
`SharedState()` returns storage-backed state intended for prefork/multi-process sharing.
87-
88-
```go title="Signature"
89-
func (app *App) State() *State
90-
func (app *App) SharedState() *SharedState
91-
```
92-
93-
See [State Management](./state.md) for usage and examples.
54+
:::caution
55+
Unlike Express, Fiber does not strip the mount prefix. Inside the mounted app, `c.Path()` still returns the full request path (`/john/doe`, not `/doe`); there is no `req.baseUrl` equivalent.
56+
:::
9457

9558
### MountPath
9659

@@ -223,6 +186,7 @@ func main() {
223186
app.RouteChain("/events").All(func(c fiber.Ctx) error {
224187
// Runs for all HTTP verbs first
225188
// Think of it as route-specific middleware!
189+
return c.Next()
226190
}).
227191
Get(func(c fiber.Ctx) error {
228192
return c.SendString("GET /events")
@@ -375,6 +339,7 @@ package main
375339

376340
import (
377341
"encoding/json"
342+
"fmt"
378343
"log"
379344

380345
"github.com/gofiber/fiber/v3"
@@ -660,7 +625,7 @@ func (app *App) Config() Config
660625

661626
## Handler
662627

663-
`Handler` returns the server handler that can be used to serve custom [`\*fasthttp.RequestCtx`](https://pkg.go.dev/github.com/valyala/fasthttp#RequestCtx) requests.
628+
`Handler` returns the server handler that can be used to serve custom [`*fasthttp.RequestCtx`](https://pkg.go.dev/github.com/valyala/fasthttp#RequestCtx) requests.
664629

665630
```go title="Signature"
666631
func (app *App) Handler() fasthttp.RequestHandler
@@ -798,6 +763,18 @@ Use `SetTLSHandler` to set [`ClientHelloInfo`](https://datatracker.ietf.org/doc/
798763
func (app *App) SetTLSHandler(tlsHandler *TLSHandler)
799764
```
800765

766+
## State / SharedState
767+
768+
`State()` returns in-process state (local to the current process).
769+
`SharedState()` returns storage-backed state intended for prefork/multi-process sharing.
770+
771+
```go title="Signature"
772+
func (app *App) State() *State
773+
func (app *App) SharedState() *SharedState
774+
```
775+
776+
See [State Management](./state.md) for usage and examples.
777+
801778
## Test
802779

803780
Testing your application is done with the `Test` method. Use this method for creating `_test.go` files or when you need to debug your routing logic. The default timeout is `1s`; to disable a timeout altogether, pass a `TestConfig` struct with `Timeout: 0`.
@@ -855,8 +832,7 @@ config := fiber.TestConfig{
855832

856833
:::caution
857834

858-
This is **not** the same as supplying an empty `TestConfig{}` to
859-
`app.Test(), but rather be the equivalent of supplying:
835+
Calling `app.Test(req)` uses the defaults above. Supplying an empty `fiber.TestConfig{}` instead is **not** equivalent; it is the same as supplying:
860836

861837
```go title="Empty TestConfig"
862838
cfg := fiber.TestConfig{
@@ -877,7 +853,11 @@ This would make a Test that has no timeout.
877853
func (app *App) Hooks() *Hooks
878854
```
879855

880-
## RebuildTree
856+
## Route Management
857+
858+
Routes are normally defined before the app starts. You can also add or remove them at runtime with the methods below, but these operations are **not thread-safe** and are performance-intensive, so use them sparingly and only in development.
859+
860+
### RebuildTree
881861

882862
The `RebuildTree` method is designed to rebuild the route tree and enable dynamic route registration. It returns a pointer to the `App` instance.
883863

@@ -887,8 +867,6 @@ func (app *App) RebuildTree() *App
887867

888868
**Note:** Use this method with caution. It is **not** thread-safe and calling it can be very performance-intensive, so it should be used sparingly and only in development mode. Avoid using it concurrently.
889869

890-
### Example Usage
891-
892870
Here’s an example of how to define and register routes dynamically:
893871

894872
```go title="Example"
@@ -921,7 +899,7 @@ func main() {
921899

922900
In this example, a new route is defined and then `RebuildTree()` is called to ensure the new route is registered and available.
923901

924-
## RemoveRoute
902+
### RemoveRoute
925903

926904
This method removes a route by path. You must call the `RebuildTree()` method after the removal to finalize the update and rebuild the routing tree.
927905
If no methods are specified, the route will be removed for all HTTP methods defined in the app. To limit removal to specific methods, provide them as additional arguments.
@@ -969,7 +947,7 @@ func main() {
969947
}
970948
```
971949

972-
## RemoveRouteByName
950+
### RemoveRouteByName
973951

974952
This method removes a route by name.
975953
If no methods are specified, the route will be removed for all HTTP methods defined in the app. To limit removal to specific methods, provide them as additional arguments.
@@ -978,11 +956,48 @@ If no methods are specified, the route will be removed for all HTTP methods defi
978956
func (app *App) RemoveRouteByName(name string, methods ...string)
979957
```
980958

981-
## RemoveRouteFunc
959+
### RemoveRouteFunc
982960

983961
This method removes a route by function having `*Route` parameter.
984962
If no methods are specified, the route will be removed for all HTTP methods defined in the app. To limit removal to specific methods, provide them as additional arguments.
985963

986964
```go title="Signature"
987965
func (app *App) RemoveRouteFunc(matchFunc func(r *Route) bool, methods ...string)
988966
```
967+
968+
## Helpers
969+
970+
### GetString
971+
972+
Returns `s` unchanged when [`Immutable`](./fiber.md#immutable) is disabled or `s` resides in read-only memory. Otherwise, it returns a detached copy using `strings.Clone`.
973+
974+
```go title="Signature"
975+
func (app *App) GetString(s string) string
976+
```
977+
978+
### GetBytes
979+
980+
Returns `b` unchanged when [`Immutable`](./fiber.md#immutable) is disabled or `b` resides in read-only memory. Otherwise, it returns a detached copy.
981+
982+
```go title="Signature"
983+
func (app *App) GetBytes(b []byte) []byte
984+
```
985+
986+
### ReloadViews
987+
988+
Reloads the configured view engine on demand by calling its `Load` method. Use this helper in development workflows (e.g., file watchers or debug-only routes) to pick up template changes without restarting the server. Returns an error if no view engine is configured or reloading fails.
989+
990+
```go title="Signature"
991+
func (app *App) ReloadViews() error
992+
```
993+
994+
```go title="Example"
995+
app := fiber.New(fiber.Config{Views: engine})
996+
997+
app.Get("/dev/reload", func(c fiber.Ctx) error {
998+
if err := app.ReloadViews(); err != nil {
999+
return err
1000+
}
1001+
return c.SendString("Templates reloaded")
1002+
})
1003+
```

docs/guide/grouping.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,29 @@ func main() {
8181
log.Fatal(app.Listen(":3000"))
8282
}
8383
```
84+
85+
## Route
86+
87+
[`Route`](../api/app.md#route) groups routes under a common prefix declared inside a single callback, with an optional name prefix. It is shorthand for nesting with `Group`.
88+
89+
```go
90+
app.Route("/api/v1", func(r fiber.Router) {
91+
r.Get("/users", handler).Name("users") // /api/v1/users (name: v1.users)
92+
r.Post("/users", handler).Name("create") // /api/v1/users (name: v1.create)
93+
}, "v1.")
94+
```
95+
96+
## RouteChain
97+
98+
When several HTTP methods share the **same path**, [`RouteChain`](../api/app.md#routechain) lets you declare the path once and chain the verb handlers. An `All` in the chain runs before the verb handlers on that path, acting as route-specific middleware.
99+
100+
```go
101+
app.RouteChain("/events").
102+
All(func(c fiber.Ctx) error { return c.Next() }). // route-local middleware
103+
Get(func(c fiber.Ctx) error { return c.SendString("GET /events") }).
104+
Post(func(c fiber.Ctx) error { return c.SendString("POST /events") })
105+
```
106+
107+
:::note
108+
Within a chain, `All` registers prefix-matched middleware (like [`app.Use`](../api/app.md#use)), not the exact-path `App.All`, so it also runs for sub-paths of the chain path.
109+
:::

0 commit comments

Comments
 (0)