π Whats New in v3
Its a draft, not finished yet.
π Welcomeβ
We are excited to announce the release of Fiber v3! π
In this guide, we'll walk you through the most important changes in Fiber v3
and show you how to migrate your existing Fiber v2
applications to Fiber v3
.
Here's a quick overview of the changes in Fiber v3
:
- π App
- πΊοΈ Router
- π§ Context
- π Binding
- ποΈ Redirect
- π Client package
- π§° Generic functions
- 𧬠Middlewares
- π Migration guide
Drop for old Go versionsβ
Fiber v3
drops support for Go versions below 1.21
. We recommend upgrading to Go 1.21
or higher to use Fiber v3
.
π Appβ
DRAFT section
We have made several changes to the Fiber app, including:
- Listen -> unified with config
- app.Config properties moved to listen config
- DisableStartupMessage
- EnablePrefork -> previously Prefork
- EnablePrintRoutes
- ListenerNetwork -> previously Network
new methodsβ
- RegisterCustomBinder
- RegisterCustomConstraint
- NewCtxFunc
removed methodsβ
- Mount -> Use app.Use() instead
- ListenTLS -> Use app.Listen() with tls.Config
- ListenTLSWithCertificate -> Use app.Listen() with tls.Config
- ListenMutualTLS -> Use app.Listen() with tls.Config
- ListenMutualTLSWithCertificate -> Use app.Listen() with tls.Config
Methods changesβ
- Test -> timeout changed to 1 second
- Listen -> has a config parameter
- Listener -> has a config parameter
CTX interface + customizableβ
πΊ Routerβ
We have slightly adapted our router interface
HTTP method registrationβ
In v2
one handler was already mandatory when the route has been registered, but this was checked at runtime and was not correctly reflected in the signature, this has now been changed in v3
to make it more explicit.
- Get(path string, handlers ...Handler) Router
+ Get(path string, handler Handler, middleware ...Handler) Router
- Head(path string, handlers ...Handler) Router
+ Head(path string, handler Handler, middleware ...Handler) Router
- Post(path string, handlers ...Handler) Router
+ Post(path string, handler Handler, middleware ...Handler) Router
- Put(path string, handlers ...Handler) Router
+ Put(path string, handler Handler, middleware ...Handler) Router
- Delete(path string, handlers ...Handler) Router
+ Delete(path string, handler Handler, middleware ...Handler) Router
- Connect(path string, handlers ...Handler) Router
+ Connect(path string, handler Handler, middleware ...Handler) Router
- Options(path string, handlers ...Handler) Router
+ Options(path string, handler Handler, middleware ...Handler) Router
- Trace(path string, handlers ...Handler) Router
+ Trace(path string, handler Handler, middleware ...Handler) Router
- Patch(path string, handlers ...Handler) Router
+ Patch(path string, handler Handler, middleware ...Handler) Router
- All(path string, handlers ...Handler) Router
+ All(path string, handler Handler, middleware ...Handler) Router
Route chainingβ
The route method is now like Express
which gives you the option of a different notation and allows you to concatenate the route declaration.
- Route(prefix string, fn func(router Router), name ...string) Router
+ Route(path string) Register
Example
app.Route("/api").Route("/user/:id?")
.Get(func(c fiber.Ctx) error {
// Get user
return c.JSON(fiber.Map{"message": "Get user", "id": c.Params("id")})
})
.Post(func(c fiber.Ctx) error {
// Create user
return c.JSON(fiber.Map{"message": "User created"})
})
.Put(func(c fiber.Ctx) error {
// Update user
return c.JSON(fiber.Map{"message": "User updated", "id": c.Params("id")})
})
.Delete(func(c fiber.Ctx) error {
// Delete user
return c.JSON(fiber.Map{"message": "User deleted", "id": c.Params("id")})
})
})
Here you can find more information.
Middleware registrationβ
We have aligned our method for middlewares closer to Express
and now also support the Use
of multiple prefixes.
Registering a subapp is now also possible via the Use
method instead of the old app.Mount
method.
Example
// register mulitple prefixes
app.Use(["/v1", "/v2"], func(c *fiber.Ctx) error {
// Middleware for /v1 and /v2
return c.Next()
})
// define subapp
api := fiber.New()
api.Get("/user", func(c *fiber.Ctx) error {
return c.SendString("User")
})
// register subapp
app.Use("/api", api)
To enable the routing changes above we had to slightly adjust the signature of the Add
method.
- Add(method, path string, handlers ...Handler) Router
+ Add(methods []string, path string, handler Handler, middleware ...Handler) Router
π§ Contextβ
DRAFT section
new methodsβ
- AutoFormat -> ExpressJs like
- Host -> ExpressJs like
- Port -> ExpressJs like
- IsProxyTrusted
- Reset
- Schema -> ExpressJs like
- SendStream -> ExpressJs like
- SendString -> ExpressJs like
- String -> ExpressJs like
- ViewBind -> instead of Bind
removed methodsβ
- AllParams -> c.Bind().URL() ?
- ParamsInt -> Params Generic
- QueryBool -> Query Generic
- QueryFloat -> Query Generic
- QueryInt -> Query Generic
- BodyParser -> c.Bind().Body()
- CookieParser -> c.Bind().Cookie()
- ParamsParser -> c.Bind().URL()
- RedirectToRoute -> c.Redirect().Route()
- RedirectBack -> c.Redirect().Back()
- ReqHeaderParser -> c.Bind().Header()
changed methodsβ
- Bind -> for Binding instead of View, us c.ViewBind()
- Format -> Param: body interface -> handlers ...ResFmt
- Redirect -> c.Redirect().To()
π Client packageβ
DRAFT section
π Bindingβ
DRAFT section
π Redirectβ
DRAFT section
π§° Generic functionsβ
DRAFT section
𧬠Middlewaresβ
CORSβ
We've made some changes to the CORS middleware to improve its functionality and flexibility. Here's what's new:
New Struct Fieldsβ
Config.AllowPrivateNetwork
: This new field is a boolean that allows you to control whether private networks are allowed. This is related to the Private Network Access (PNA) specification from the Web Incubator Community Group (WICG). When set totrue
, the CORS middleware will allow CORS preflight requests from private networks and respond with theAccess-Control-Allow-Private-Network: true
header. This could be useful in development environments or specific use cases, but should be done with caution due to potential security risks.
Updated Struct Fieldsβ
We've updated several fields from a single string (containing comma-separated values) to slices, allowing for more explicit declaration of multiple values. Here are the updated fields:
Config.AllowOrigins
: Now accepts a slice of strings, each representing an allowed origin.Config.AllowMethods
: Now accepts a slice of strings, each representing an allowed method.Config.AllowHeaders
: Now accepts a slice of strings, each representing an allowed header.Config.ExposeHeaders
: Now accepts a slice of strings, each representing an exposed header.
Sessionβ
DRAFT section
Filesystemβ
DRAFT section
Monitorβ
DRAFT section
Monitor middleware is now in Contrib package.
π Migration guideβ
π Appβ
πΊ Routerβ
π§ Contextβ
π Parserβ
π Redirectβ
π Client packageβ
𧬠Middlewaresβ
CORSβ
The CORS middleware has been updated to use slices instead of strings for the AllowOrigins
, AllowMethods
, AllowHeaders
, and ExposeHeaders
fields. Here's how you can update your code:
// Before
app.Use(cors.New(cors.Config{
AllowOrigins: "https://example.com,https://example2.com",
AllowMethods: strings.Join([]string{fiber.MethodGet, fiber.MethodPost}, ","),
AllowHeaders: "Content-Type",
ExposeHeaders: "Content-Length",
}))
// After
app.Use(cors.New(cors.Config{
AllowOrigins: []string{"https://example.com", "https://example2.com"},
AllowMethods: []string{fiber.MethodGet, fiber.MethodPost},
AllowHeaders: []string{"Content-Type"},
ExposeHeaders: []string{"Content-Length"},
}))
...