πŸš€
App
The app instance conventionally denotes the Fiber application.

Static

Use the Static method to serve static files such as images, CSS, and JavaScript.
By default, Static will serve index.html files in response to a request on a directory.
Signature
1
func (app *App) Static(prefix, root string, config ...Static) Router
Copied!
Use the following code to serve files in a directory named ./public
Example
1
app.Static("/", "./public")
2
​
3
// => http://localhost:3000/hello.html
4
// => http://localhost:3000/js/jquery.js
5
// => http://localhost:3000/css/style.css
Copied!
Example
1
// Serve files from multiple directories
2
app.Static("/", "./public")
3
​
4
// Serve files from "./files" directory:
5
app.Static("/", "./files")
Copied!
You can use any virtual path prefix (where the path does not actually exist in the file system) for files that are served by the Static method, specify a prefix path for the static directory, as shown below:
Example
1
app.Static("/static", "./public")
2
​
3
// => http://localhost:3000/static/hello.html
4
// => http://localhost:3000/static/js/jquery.js
5
// => http://localhost:3000/static/css/style.css
Copied!
If you want to have a little bit more control regarding the settings for serving static files. You could use the fiber.Static struct to enable specific settings.
fiber.Static{}
1
// Static defines configuration options when defining static assets.
2
type Static struct {
3
// When set to true, the server tries minimizing CPU usage by caching compressed files.
4
// This works differently than the github.com/gofiber/compression middleware.
5
// Optional. Default value false
6
Compress bool `json:"compress"`
7
​
8
// When set to true, enables byte range requests.
9
// Optional. Default value false
10
ByteRange bool `json:"byte_range"`
11
​
12
// When set to true, enables directory browsing.
13
// Optional. Default value false.
14
Browse bool `json:"browse"`
15
​
16
// The name of the index file for serving a directory.
17
// Optional. Default value "index.html".
18
Index string `json:"index"`
19
​
20
// Expiration duration for inactive file handlers.
21
// Use a negative time.Duration to disable it.
22
//
23
// Optional. Default value 10 * time.Second.
24
CacheDuration time.Duration `json:"cache_duration"`
25
​
26
// The value for the Cache-Control HTTP-header
27
// that is set on the file response. MaxAge is defined in seconds.
28
//
29
// Optional. Default value 0.
30
MaxAge int `json:"max_age"`
31
​
32
// Next defines a function to skip this middleware when returned true.
33
//
34
// Optional. Default: nil
35
Next func(c *Ctx) bool
36
}
Copied!
Example
1
// Custom config
2
app.Static("/", "./public", fiber.Static{
3
Compress: true,
4
ByteRange: true,
5
Browse: true,
6
Index: "john.html"
7
CacheDuration: 10 * time.Second,
8
MaxAge: 3600,
9
})
Copied!

Route Handlers

Registeres a route bound to a specific HTTP method.
Signatures
1
// HTTP methods
2
func (app *App) Get(path string, handlers ...Handler) Router
3
func (app *App) Head(path string, handlers ...Handler) Router
4
func (app *App) Post(path string, handlers ...Handler) Router
5
func (app *App) Put(path string, handlers ...Handler) Router
6
func (app *App) Delete(path string, handlers ...Handler) Router
7
func (app *App) Connect(path string, handlers ...Handler) Router
8
func (app *App) Options(path string, handlers ...Handler) Router
9
func (app *App) Trace(path string, handlers ...Handler) Router
10
func (app *App) Patch(path string, handlers ...Handler) Router
11
​
12
// Add allows you to specifiy a method as value
13
func (app *App) Add(method, path string, handlers ...Handler) Router
14
​
15
// All will register the route on all HTTP methods
16
// Almost the same as app.Use but not bound to prefixes
17
func (app *App) All(path string, handlers ...Handler) Router
Copied!
Example
1
// Simple GET handler
2
app.Get("/api/list", func(c *fiber.Ctx)error{
3
return c.SendString("I'm a GET request!")
4
})
5
​
6
// Simple POST handler
7
app.Post("/api/register", func(c *fiber.Ctx) error {
8
return c.SendString("I'm a POST request!")
9
})
Copied!
Use 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
Signatures
1
func (app *App) Use(args ...interface{}) Router
Copied!
Example
1
// Match any request
2
app.Use(func(c *fiber.Ctx) error {
3
return c.Next()
4
})
5
​
6
// Match request starting with /api
7
app.Use("/api", func(c *fiber.Ctx) error {
8
return c.Next()
9
})
10
​
11
// Attach multiple handlers
12
app.Use("/api",func(c *fiber.Ctx) error {
13
c.Set("X-Custom-Header", random.String(32))
14
return c.Next()
15
}, func(c *fiber.Ctx) error {
16
return c.Next()
17
})
Copied!

Mount

You can Mount Fiber instance by creating a *Mount
Signature
1
func (a *App) Mount(prefix string, app *App) Router
Copied!
Example
1
func main() {
2
micro := fiber.New()
3
micro.Get("/doe", func(c *fiber.Ctx) error {
4
return c.SendStatus(fiber.StatusOK)
5
})
6
​
7
app := fiber.New()
8
app.Mount("/john", micro) // GET /john/doe -> 200 OK
9
​
10
log.Fatal(app.Listen(":3000"))
11
}
Copied!

Group

You can group routes by creating a *Group struct.
Signature
1
func (app *App) Group(prefix string, handlers ...Handler) Router
Copied!
Example
1
func main() {
2
app := fiber.New()
3
​
4
api := app.Group("/api", handler) // /api
5
​
6
v1 := api.Group("/v1", handler) // /api/v1
7
v1.Get("/list", handler) // /api/v1/list
8
v1.Get("/user", handler) // /api/v1/user
9
​
10
v2 := api.Group("/v2", handler) // /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!

Server

Server returns the underlying fasthttp server​
1
func (app *App) Server() *fasthttp.Server
Copied!
1
func main() {
2
app := fiber.New()
3
​
4
app.Server().MaxConnsPerIP = 1
5
​
6
// ...
7
}
Copied!

Stack

This method returns the original router stack
Signature
1
func (app *App) Stack() [][]*Route
Copied!
Example
1
var handler = func(c *fiber.Ctx) error { return nil }
2
​
3
func main() {
4
app := fiber.New()
5
​
6
app.Get("/john/:age", handler)
7
app.Post("/register", handler)
8
​
9
data, _ := json.MarshalIndent(app.Stack(), "", " ")
10
fmt.Println(string(data))
11
​
12
app.Listen(":3000")
13
}
Copied!
Result
1
[
2
[
3
{
4
"method": "GET",
5
"path": "/john/:age",
6
"params": [
7
"age"
8
]
9
}
10
],
11
[
12
{
13
"method": "HEAD",
14
"path": "/john/:age",
15
"params": [
16
"age"
17
]
18
}
19
],
20
[
21
{
22
"method": "POST",
23
"path": "/register",
24
"params": null
25
}
26
]
27
]
Copied!

Config

Config returns the app config as value ( read-only ).
Signature
1
func (app *App) Config() Config
Copied!

Handler

Handler returns the server handler that can be used to serve custom *fasthttp.RequestCtx requests.
Signature
1
func (app *App) Handler() fasthttp.RequestHandler
Copied!

Listen

Listen serves HTTP requests from the given address.
Signature
1
func (app *App) Listen(addr string) error
Copied!
Examples
1
// Listen on port :8080
2
app.Listen(":8080")
3
​
4
// Custom host
5
app.Listen("127.0.0.1:8080")
Copied!

ListenTLS

ListenTLS serves HTTPs requests from the given address using certFile and keyFile paths to as TLS certificate and key file.
Signature
1
func (app *App) ListenTLS(addr, certFile, keyFile string) error
Copied!
Examples
1
app.ListenTLS(":443", "./cert.pem", "./cert.key");
Copied!
Using ListenTLS defaults to the following config ( use Listener to provide your own config )
Default *tls.Config
1
&tls.Config{
2
MinVersion: tls.VersionTLS12,
3
PreferServerCipherSuites: true,
4
Certificates: []tls.Certificate{
5
cert,
6
},
7
}
Copied!

Listener

You can pass your own net.Listener using the Listener method. This method can be used to enable TLS/HTTPS with a custom tls.Config.
Signature
1
func (app *App) Listener(ln net.Listener) error
Copied!
Example
1
ln, _ := net.Listen("tcp", ":3000")
2
​
3
cer, _:= tls.LoadX509KeyPair("server.crt", "server.key")
4
​
5
ln = tls.NewListener(ln, &tls.Config{Certificates: []tls.Certificate{cer}})
6
​
7
app.Listener(ln)
Copied!

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.
Signature
1
func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error)
Copied!
Example
1
// Create route with GET method for test:
2
app.Get("/", func(c *fiber.Ctx) error {
3
fmt.Println(c.BaseURL()) // => http://google.com
4
fmt.Println(c.Get("X-Custom-Header")) // => hi
5
​
6
return c.SendString("hello, World!")
7
})
8
​
9
// http.Request
10
req := httptest.NewRequest("GET", "http://google.com", nil)
11
req.Header.Set("X-Custom-Header", "hi")
12
​
13
// http.Response
14
resp, _ := app.Test(req)
15
​
16
// Do something with results:
17
if resp.StatusCode == fiber.StatusOK {
18
body, _ := ioutil.ReadAll(resp.Body)
19
fmt.Println(string(body)) // => Hello, World!
20
}
Copied!
Last modified 3mo ago