You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/app.md
+73-58Lines changed: 73 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,58 +3,29 @@ id: app
3
3
title: 🚀 App
4
4
description: The `App` type represents your Fiber application.
5
5
sidebar_position: 2
6
+
toc_max_heading_level: 4
6
7
---
7
8
8
9
import Reference from '@site/src/components/reference';
9
10
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(sstring) 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
-
iferr:= app.ReloadViews(); err != nil {
41
-
return err
42
-
}
43
-
return c.SendString("Templates reloaded")
44
-
})
45
-
```
46
-
47
11
## Routing
48
12
49
13
import RoutingHandler from './../partials/routing/handler.md';
14
+
import RoutingUse from './../partials/routing/use.md';
50
15
51
16
### Route Handlers
52
17
53
18
<RoutingHandler />
54
19
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
+
55
26
### Mounting
56
27
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).
58
29
59
30
```go title="Example"
60
31
package main
@@ -80,17 +51,9 @@ func main() {
80
51
}
81
52
```
82
53
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.
`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.
664
629
665
630
```go title="Signature"
666
631
func (app *App) Handler() fasthttp.RequestHandler
@@ -798,6 +763,18 @@ Use `SetTLSHandler` to set [`ClientHelloInfo`](https://datatracker.ietf.org/doc/
`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
+
801
778
## Test
802
779
803
780
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{
855
832
856
833
:::caution
857
834
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:
860
836
861
837
```go title="Empty TestConfig"
862
838
cfg:= fiber.TestConfig{
@@ -877,7 +853,11 @@ This would make a Test that has no timeout.
877
853
func(app *App) Hooks() *Hooks
878
854
```
879
855
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
881
861
882
862
The `RebuildTree` method is designed to rebuild the route tree and enable dynamic route registration. It returns a pointer to the `App` instance.
**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.
889
869
890
-
### Example Usage
891
-
892
870
Here’s an example of how to define and register routes dynamically:
893
871
894
872
```go title="Example"
@@ -921,7 +899,7 @@ func main() {
921
899
922
900
In this example, a new route is defined and then `RebuildTree()` is called to ensure the new route is registered and available.
923
901
924
-
## RemoveRoute
902
+
###RemoveRoute
925
903
926
904
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.
927
905
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() {
969
947
}
970
948
```
971
949
972
-
## RemoveRouteByName
950
+
###RemoveRouteByName
973
951
974
952
This method removes a route by name.
975
953
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
This method removes a route by function having `*Route` parameter.
984
962
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.
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.
Copy file name to clipboardExpand all lines: docs/guide/grouping.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,3 +81,29 @@ func main() {
81
81
log.Fatal(app.Listen(":3000"))
82
82
}
83
83
```
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`.
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.
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.
0 commit comments