π App
Routingβ
Route Handlersβ
Registers a route bound to a specific HTTP method.
// HTTP methods
func (app *App) Get(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Head(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Post(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Put(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Delete(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Connect(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Options(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Trace(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Patch(path string, handler Handler, middlewares ...Handler) Router
// Add allows you to specify a method as value
func (app *App) Add(method, path string, handler Handler, middlewares ...Handler) Router
// All will register the route on all HTTP methods
// Almost the same as app.Use but not bound to prefixes
func (app *App) All(path string, handler Handler, middlewares ...Handler) Router
// Simple GET handler
app.Get("/api/list", func(c fiber.Ctx) error {
return c.SendString("I'm a GET request!")
})
// Simple POST handler
app.Post("/api/register", func(c fiber.Ctx) error {
return c.SendString("I'm a POST request!")
})
Can be used for middleware packages and prefix catchers. These routes will only match the beginning of each path i.e. /john
will match /john/doe
, /johnnnnn
etc
func (app *App) Use(args ...any) Router
// Different usage variations
func (app *App) Use(handler Handler, middlewares ...Handler) Router
func (app *App) Use(path string, handler Handler, middlewares ...Handler) Router
func (app *App) Use(paths []string, handler Handler, middlewares ...Handler) Router
func (app *App) Use(path string, app *App) Router
// Match any request
app.Use(func(c fiber.Ctx) error {
return c.Next()
})
// Match request starting with /api
app.Use("/api", func(c fiber.Ctx) error {
return c.Next()
})
// Match requests starting with /api or /home (multiple-prefix support)
app.Use([]string{"/api", "/home"}, func(c fiber.Ctx) error {
return c.Next()
})
// Attach multiple handlers
app.Use("/api", func(c fiber.Ctx) error {
c.Set("X-Custom-Header", random.String(32))
return c.Next()
}, func(c fiber.Ctx) error {
return c.Next()
})
// Mount a sub-app
app.Use("/api", api)
Mountingβ
You can Mount Fiber instance using the app.Use
method similar to express
.
func main() {
app := fiber.New()
micro := fiber.New()
app.Use("/john", micro) // GET /john/doe -> 200 OK
micro.Get("/doe", func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})
log.Fatal(app.Listen(":3000"))
}
MountPathβ
The MountPath
property contains one or more path patterns on which a sub-app was mounted.
func (app *App) MountPath() string
func main() {
app := fiber.New()
one := fiber.New()
two := fiber.New()
three := fiber.New()
two.Use("/three", three)
one.Use("/two", two)
app.Use("/one", one)
one.MountPath() // "/one"
two.MountPath() // "/one/two"
three.MountPath() // "/one/two/three"
app.MountPath() // ""
}
Mounting order is important for MountPath. If you want to get mount paths properly, you should start mounting from the deepest app.
Groupβ
You can group routes by creating a *Group
struct.
func (app *App) Group(prefix string, handlers ...Handler) Router
func main() {
app := fiber.New()
api := app.Group("/api", handler) // /api
v1 := api.Group("/v1", handler) // /api/v1
v1.Get("/list", handler) // /api/v1/list
v1.Get("/user", handler) // /api/v1/user
v2 := api.Group("/v2", handler) // /api/v2
v2.Get("/list", handler) // /api/v2/list
v2.Get("/user", handler) // /api/v2/user
log.Fatal(app.Listen(":3000"))
}
Routeβ
Returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware.
Similar to Express
.
func (app *App) Route(path string) Register
Click here to see the Register
interface
type Register interface {
All(handler Handler, middleware ...Handler) Register
Get(handler Handler, middleware ...Handler) Register
Head(handler Handler, middleware ...Handler) Register
Post(handler Handler, middleware ...Handler) Register
Put(handler Handler, middleware ...Handler) Register
Delete(handler Handler, middleware ...Handler) Register
Connect(handler Handler, middleware ...Handler) Register
Options(handler Handler, middleware ...Handler) Register
Trace(handler Handler, middleware ...Handler) Register
Patch(handler Handler, middleware ...Handler) Register
Add(methods []string, handler Handler, middleware ...Handler) Register
Route(path string) Register
}
func main() {
app := fiber.New()
// use `Route` as chainable route declaration method
app.Route("/test").Get(func(c fiber.Ctx) error {
return c.SendString("GET /test")
})
app.Route("/events").all(func(c fiber.Ctx) error {
// runs for all HTTP verbs first
// think of it as route specific middleware!
})
.get(func(c fiber.Ctx) error {
return c.SendString("GET /events")
})
.post(func(c fiber.Ctx) error {
// maybe add a new event...
})
// combine multiple routes
app.Route("/v2").Route("/user").Get(func(c fiber.Ctx) error {
return c.SendString("GET /v2/user")
})
// use multiple methods
app.Route("/api").Get(func(c fiber.Ctx) error {
return c.SendString("GET /api")
}).Post(func(c fiber.Ctx) error {
return c.SendString("POST /api")
})
log.Fatal(app.Listen(":3000"))
}
HandlersCountβ
This method returns the amount of registered handlers.
func (app *App) HandlersCount() uint32
Stackβ
This method returns the original router stack
func (app *App) Stack() [][]*Route
var handler = func(c fiber.Ctx) error { return nil }
func main() {
app := fiber.New()
app.Get("/john/:age", handler)
app.Post("/register", handler)
data, _ := json.MarshalIndent(app.Stack(), "", " ")
fmt.Println(string(data))
app.Listen(":3000")
}
Click here to see the result
[
[
{
"method": "GET",
"path": "/john/:age",
"params": [
"age"
]
}
],
[
{
"method": "HEAD",
"path": "/john/:age",
"params": [
"age"
]
}
],
[
{
"method": "POST",
"path": "/register",
"params": null
}
]
]
Nameβ
This method assigns the name of latest created route.
func (app *App) Name(name string) Router
var handler = func(c fiber.Ctx) error { return nil }
func main() {
app := fiber.New()
app.Get("/", handler)
app.Name("index")
app.Get("/doe", handler).Name("home")
app.Trace("/tracer", handler).Name("tracert")
app.Delete("/delete", handler).Name("delete")
a := app.Group("/a")
a.Name("fd.")
a.Get("/test", handler).Name("test")
data, _ := json.MarshalIndent(app.Stack(), "", " ")
fmt.Print(string(data))
app.Listen(":3000")
}
Click here to see the result
[
[
{
"method": "GET",
"name": "index",
"path": "/",
"params": null
},
{
"method": "GET",
"name": "home",
"path": "/doe",
"params": null
},
{
"method": "GET",
"name": "fd.test",
"path": "/a/test",
"params": null
}
],
[
{
"method": "HEAD",
"name": "",
"path": "/",
"params": null
},
{
"method": "HEAD",
"name": "",
"path": "/doe",
"params": null
},
{
"method": "HEAD",
"name": "",
"path": "/a/test",
"params": null
}
],
null,
null,
[
{
"method": "DELETE",
"name": "delete",
"path": "/delete",
"params": null
}
],
null,
null,
[
{
"method": "TRACE",
"name": "tracert",
"path": "/tracer",
"params": null
}
],
null
]
GetRouteβ
This method gets the route by name.
func (app *App) GetRoute(name string) Route
var handler = func(c fiber.Ctx) error { return nil }
func main() {
app := fiber.New()
app.Get("/", handler).Name("index")
data, _ := json.MarshalIndent(app.GetRoute("index"), "", " ")
fmt.Print(string(data))
app.Listen(":3000")
}
Click here to see the result
{
"method": "GET",
"name": "index",
"path": "/",
"params": null
}
GetRoutesβ
This method gets all routes.
func (app *App) GetRoutes(filterUseOption ...bool) []Route
When filterUseOption equal to true, it will filter the routes registered by the middleware.
func main() {
app := fiber.New()
app.Post("/", func (c fiber.Ctx) error {
return c.SendString("Hello, World!")
}).Name("index")
data, _ := json.MarshalIndent(app.GetRoutes(true), "", " ")
fmt.Print(string(data))
}
Click here to see the result
[
{
"method": "POST",
"name": "index",
"path": "/",
"params": null
}
]
Configβ
Config returns the app config as value ( read-only ).
func (app *App) Config() Config
Handlerβ
Handler returns the server handler that can be used to serve custom \*fasthttp.RequestCtx
requests.
func (app *App) Handler() fasthttp.RequestHandler
ErrorHandlerβ
Errorhandler executes the process which was defined for the application in case of errors, this is used in some cases in middlewares.
func (app *App) ErrorHandler(ctx Ctx, err error) error
NewCtxFuncβ
NewCtxFunc allows to customize the ctx struct as we want.
func (app *App) NewCtxFunc(function func(app *App) CustomCtx)
type CustomCtx struct {
DefaultCtx
}
// Custom method
func (c *CustomCtx) Params(key string, defaultValue ...string) string {
return "prefix_" + c.DefaultCtx.Params(key)
}
app := New()
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx {
return &CustomCtx{
DefaultCtx: *NewDefaultCtx(app),
}
})
// curl http://localhost:3000/123
app.Get("/:id", func(c Ctx) error {
// use custom method - output: prefix_123
return c.SendString(c.Params("id"))
})
RegisterCustomBinderβ
You can register custom binders to use as Bind().Custom("name")
.
They should be compatible with CustomBinder interface.
func (app *App) RegisterCustomBinder(binder CustomBinder)
app := fiber.New()
// My custom binder
customBinder := &customBinder{}
// Name of custom binder, which will be used as Bind().Custom("name")
func (*customBinder) Name() string {
return "custom"
}
// Is used in the Body Bind method to check if the binder should be used for custom mime types
func (*customBinder) MIMETypes() []string {
return []string{"application/yaml"}
}
// Parse the body and bind it to the out interface
func (*customBinder) Parse(c Ctx, out any) error {
// parse yaml body
return yaml.Unmarshal(c.Body(), out)
}
// Register custom binder
app.RegisterCustomBinder(customBinder)
// curl -X POST http://localhost:3000/custom -H "Content-Type: application/yaml" -d "name: John"
app.Post("/custom", func(c Ctx) error {
var user User
// output: {Name:John}
// Custom binder is used by the name
if err := c.Bind().Custom("custom", &user); err != nil {
return err
}
// ...
return c.JSON(user)
})
// curl -X POST http://localhost:3000/normal -H "Content-Type: application/yaml" -d "name: Doe"
app.Post("/normal", func(c Ctx) error {
var user User
// output: {Name:Doe}
// Custom binder is used by the mime type
if err := c.Bind().Body(&user); err != nil {
return err
}
// ...
return c.JSON(user)
})
RegisterCustomConstraintβ
RegisterCustomConstraint allows to register custom constraint.
func (app *App) RegisterCustomConstraint(constraint CustomConstraint)
See Custom Constraint section for more information.
SetTLSHandlerβ
Use SetTLSHandler to set ClientHelloInfo when using TLS with Listener.
func (app *App) SetTLSHandler(tlsHandler *TLSHandler)
Testβ
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
if you want to disable a timeout altogether, pass -1
as a second argument.
func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error)
// Create route with GET method for test:
app.Get("/", func(c fiber.Ctx) error {
fmt.Println(c.BaseURL()) // => http://google.com
fmt.Println(c.Get("X-Custom-Header")) // => hi
return c.SendString("hello, World!")
})
// http.Request
req := httptest.NewRequest("GET", "http://google.com", nil)
req.Header.Set("X-Custom-Header", "hi")
// http.Response
resp, _ := app.Test(req)
// Do something with results:
if resp.StatusCode == fiber.StatusOK {
body, _ := io.ReadAll(resp.Body)
fmt.Println(string(body)) // => Hello, World!
}
Hooksβ
Hooks is a method to return hooks property.
func (app *App) Hooks() *Hooks
RebuildTreeβ
The RebuildTree method is designed to rebuild the route tree and enable dynamic route registration. It returns a pointer to the App instance.
func (app *App) RebuildTree() *App
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.
Example Usageβ
Hereβs an example of how to define and register routes dynamically:
app.Get("/define", func(c Ctx) error { // Define a new route dynamically
app.Get("/dynamically-defined", func(c Ctx) error { // Adding a dynamically defined route
return c.SendStatus(http.StatusOK)
})
app.RebuildTree() // Rebuild the route tree to register the new route
return c.SendStatus(http.StatusOK)
})
In this example, a new route is defined and then RebuildTree()
is called to make sure the new route is registered and available.