🚀 App

    By default, Static will serve files in response to a request on a directory.

    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 .

    1. // HTTP methods
    2. func (app *App) Head(path string, handlers ...Handler) Router
    3. func (app *App) Post(path string, handlers ...Handler) Router
    4. func (app *App) Put(path string, handlers ...Handler) Router
    5. func (app *App) Delete(path string, handlers ...Handler) Router
    6. func (app *App) Connect(path string, handlers ...Handler) Router
    7. func (app *App) Options(path string, handlers ...Handler) Router
    8. func (app *App) Patch(path string, handlers ...Handler) Router
    9. // Add allows you to specifiy a method as value
    10. func (app *App) Add(method, path string, handlers ...Handler) Router
    11. // All will register the route on all HTTP methods
    12. // Almost the same as app.Use but not bound to prefixes
    13. func (app *App) All(path string, handlers ...Handler) Router
    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. }

    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

    Handler

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

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

    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}})

    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.