description
The app instance conventionally denotes the Fiber application.

📦 App

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.
  1. func (app *App) Static(prefix, root string, config ...Static) Router

Use the following code to serve files in a directory named ./public

  1. app.Static("/", "./public")
  2. // => http://localhost:3000/hello.html
  3. // => http://localhost:3000/js/jquery.js
  4. // => http://localhost:3000/css/style.css
  1. // Serve files from multiple directories
  2. app.Static("/", "./public")
  3. // Serve files from "./files" directory:
  4. app.Static("/", "./files")

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:

  1. app.Static("/static", "./public")
  2. // => http://localhost:3000/static/hello.html
  3. // => http://localhost:3000/static/js/jquery.js
  4. // => http://localhost:3000/static/css/style.css

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.

  1. // Static represents settings for serving static files
  2. type Static struct {
  3. // Transparently compresses responses if set to true
  4. // This works differently than the github.com/gofiber/compression middleware
  5. // The server tries minimizing CPU usage by caching compressed files.
  6. // It adds ".fiber.gz" suffix to the original file name.
  7. // Optional. Default value false
  8. Compress bool
  9. // Enables byte-range requests if set to true.
  10. // Optional. Default value false
  11. ByteRange bool
  12. // Enable directory browsing.
  13. // Optional. Default value false.
  14. Browse bool
  15. // File to serve when requesting a directory path.
  16. // Optional. Default value "index.html".
  17. Index string
  18. }
  1. // Custom config
  2. app.Static("/", "./public", fiber.Static{
  3. Compress: true,
  4. ByteRange: true,
  5. Browse: true,
  6. Index: "john.html"
  7. })

Route Handlers

Registeres a route bound to a specific HTTP method.

  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. // Add allows you to specifiy a method as value
  12. func (app *App) Add(method, path string, handlers ...Handler) Router
  13. // All will register the route on all HTTP methods
  14. // Almost the same as app.Use but not bound to prefixes
  15. func (app *App) All(path string, handlers ...Handler) Router
  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. // Simple POST handler
  6. app.Post("/api/register", func(c *fiber.Ctx) error {
  7. return c.SendString("I'm a POST request!")
  8. })

Use can be used for middleware packages and prefix catchers. These routes will only match the beggining of each path i.e. “/john” will match “/john/doe”, “/johnnnn”

  1. // Match any request
  2. app.Use(func(c *fiber.Ctx) error {
  3. return c.Next()
  4. })
  5. // Match request starting with /api
  6. app.Use("/api", func(c *fiber.Ctx) error {
  7. return c.Next()
  8. })
  9. // Attach multiple handlers
  10. app.Use("/api",func(c *fiber.Ctx) error {
  11. c.Set("X-Custom-Header", random.String(32))
  12. return c.Next()
  13. }, func(c *fiber.Ctx) error {
  14. return c.Next()
  15. })

Group

You can group routes by creating a *Group struct.

Signature

  1. func (app *App) Group(prefix string, handlers ...Handler) Router

Example

  1. func main() {
  2. app := fiber.New()
  3. api := app.Group("/api", handler) // /api
  4. v1 := api.Group("/v1", handler) // /api/v1
  5. v1.Get("/list", handler) // /api/v1/list
  6. v1.Get("/user", handler) // /api/v1/user
  7. v2 := api.Group("/v2", handler) // /api/v2
  8. v2.Get("/list", handler) // /api/v2/list
  9. v2.Get("/user", handler) // /api/v2/user
  10. app.Listen(3000)
  11. }

Stack

This method returns the original router stack

  1. func (app *App) Stack() [][]*Route
  1. var handler = func(c *fiber.Ctx) {}
  2. func main() {
  3. app := fiber.New()
  4. app.Get("/john", handler)
  5. app.Post("/register", handler)
  6. data, _ := json.MarshalIndent(app.Stack(), "", " ")
  7. fmt.Println(string(data))
  8. }
  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. ]

Config

Config returns the app config as value ( read-only ).

  1. func (app *App) Config() Config

Handler

Handler returns the server handler that can be used to serve custom *fasthttp.RequestCtx requests.

  1. func (app *App) Handler() fasthttp.RequestHandler

Listen

Listen serves HTTP requests from the given address.

  1. func (app *App) Listen(addr string) error
  1. // Listen on port :8080
  2. app.Listen(":8080")
  3. // Custom host
  4. app.Listen("127.0.0.1:8080")

Listener

You can pass your own net.Listener using the Listener method. This method can be used to enable TLS/HTTPS.

  1. func (app *App) Listener(ln net.Listener) error
  1. ln, _ := net.Listen("tcp", ":3000")
  2. cer, _:= tls.LoadX509KeyPair("server.crt", "server.key")
  3. ln = tls.NewListener(ln, &tls.Config{Certificates: []tls.Certificate{cer}})
  4. app.Listener(ln)

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 200ms if you want to disable a timeout altogether, pass -1 as a second argument.

  1. func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error)
  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. return c.SendString("hello, World!")
  6. })
  7. // http.Request
  8. req := httptest.NewRequest("GET", "http://google.com", nil)
  9. req.Header.Set("X-Custom-Header", "hi")
  10. // http.Response
  11. resp, _ := app.Test(req)
  12. // Do something with results:
  13. if resp.StatusCode == 200 {
  14. body, _ := ioutil.ReadAll(resp.Body)
  15. fmt.Println(string(body)) // => Hello, World!
  16. }