FileSystem
Filesystem middleware for Fiber that enables you to serve files from a directory.
⚠️ :params & :optionals? within the prefix path are not supported!

Table of Contents

Signatures

1
func New(config Config) fiber.Handler
Copied!

Examples

Import the middleware package that is part of the Fiber web framework
1
import (
2
"github.com/gofiber/fiber/v2"
3
"github.com/gofiber/fiber/v2/middleware/filesystem"
4
)
Copied!
After you initiate your Fiber app, you can use the following possibilities:
1
// Provide a minimal config
2
app.Use(filesystem.New(filesystem.Config{
3
Root: http.Dir("./assets")
4
}))
5
​
6
// Or extend your config for customization
7
app.Use(filesystem.New(filesystem.Config{
8
Root: http.Dir("./assets"),
9
Browse: true,
10
Index: "index.html",
11
NotFoundFile: "404.html",
12
MaxAge: 3600,
13
}))
Copied!

pkger

1
package main
2
​
3
import (
4
"github.com/gofiber/fiber/v2"
5
"github.com/gofiber/fiber/v2/middleware/filesystem"
6
​
7
"github.com/markbates/pkger"
8
)
9
​
10
func main() {
11
app := fiber.New()
12
​
13
app.Use("/assets", filesystem.New(filesystem.Config{
14
Root: pkger.Dir("/assets"),
15
})
16
​
17
log.Fatal(app.Listen(":3000"))
18
}
Copied!

packr

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

go.rice

1
package main
2
​
3
import (
4
"github.com/gofiber/fiber/v2"
5
"github.com/gofiber/fiber/v2/middleware/filesystem"
6
​
7
"github.com/GeertJohan/go.rice"
8
)
9
​
10
func main() {
11
app := fiber.New()
12
​
13
app.Use("/assets", filesystem.New(filesystem.Config{
14
Root: rice.MustFindBox("assets").HTTPBox(),
15
})
16
​
17
log.Fatal(app.Listen(":3000"))
18
}
Copied!

fileb0x

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

statik

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

Config

1
// Config defines the config for middleware.
2
type Config struct {
3
// Next defines a function to skip this middleware when returned true.
4
//
5
// Optional. Default: nil
6
Next func(c *fiber.Ctx) bool
7
​
8
// Root is a FileSystem that provides access
9
// to a collection of files and directories.
10
//
11
// Required. Default: nil
12
Root http.FileSystem `json:"-"`
13
​
14
// Enable directory browsing.
15
//
16
// Optional. Default: false
17
Browse bool `json:"browse"`
18
​
19
// Index file for serving a directory.
20
//
21
// Optional. Default: "index.html"
22
Index string `json:"index"`
23
​
24
// The value for the Cache-Control HTTP-header
25
// that is set on the file response. MaxAge is defined in seconds.
26
//
27
// Optional. Default value 0.
28
MaxAge int `json:"max_age"`
29
​
30
// File to return if path is not found. Useful for SPA's.
31
//
32
// Optional. Default: ""
33
NotFoundFile string `json:"not_found_file"`
34
}
Copied!

Default Config

1
var ConfigDefault = Config{
2
Next: nil,
3
Root: nil,
4
Browse: false,
5
Index: "/index.html",
6
MaxAge: 0,
7
}
Copied!
Last modified 7mo ago