FileSystem

    Examples

    Import the middleware package that is part of the

    1. "github.com/gofiber/fiber/v2"
    2. "github.com/gofiber/fiber/v2/middleware/filesystem"
    3. )
    1. // Provide a minimal config
    2. app.Use(filesystem.New(filesystem.Config{
    3. Root: http.Dir("./assets")
    4. }))
    5. // Or extend your config for customization
    6. app.Use(filesystem.New(filesystem.Config{
    7. Root: http.Dir("./assets"),
    8. Index: "index.html",
    9. Browse: true,
    10. NotFoundFile: "404.html"
    11. }))

    Default Config

    1. var ConfigDefault = Config{
    2. Next: nil,
    3. Root: nil,
    4. Index: "/index.html",
    5. Browse: false,

    1. package main
    2. import (
    3. "github.com/gofiber/fiber/v2"
    4. "github.com/gofiber/fiber/v2/middleware/filesystem"
    5. "github.com/gobuffalo/packr/v2"
    6. )
    7. func main() {
    8. app := fiber.New()
    9. app.Use("/assets", filesystem.New(filesystem.Config{
    10. Root: packr.New("Assets Box", "/assets"),
    11. })
    12. app.Listen(":3000")
    13. }

    go.rice

    1. package main
    2. import (
    3. "github.com/gofiber/fiber/v2"
    4. "github.com/gofiber/fiber/v2/middleware/filesystem"
    5. "<Your go module>/myEmbeddedFiles"
    6. )
    7. func main() {
    8. app := fiber.New()
    9. app.Use("/assets", filesystem.New(filesystem.Config{
    10. Root: myEmbeddedFiles.HTTP,
    11. app.Listen(":3000")
    12. }

    statik

    1. package main
    2. import (
    3. "github.com/gofiber/fiber/v2"
    4. "github.com/gofiber/fiber/v2/middleware/filesystem"
    5. "<Your go module>/statik"
    6. fs "github.com/rakyll/statik/fs"
    7. )
    8. func main() {
    9. statik, err := fs.New()
    10. if err != nil {
    11. panic(err)
    12. }
    13. app := fiber.New()
    14. app.Use("/", filesystem.New(filesystem.Config{
    15. Root: statikFS,
    16. })
    17. app.Listen(":3000")