Static
Use the Static method to serve static files such as images, CSS and JavaScript.
By default, Static will serveindex.html
files in response to a request on a directory.
- app.Static(prefix, root string, config ...Static) // => with prefix
Use the following code to serve files in a directory named ./public
- app.Static("/", "./public")
- // => http://localhost:3000/hello.html
- // => http://localhost:3000/js/jquery.js
- // => http://localhost:3000/css/style.css
To serve from multiple directories, you can use Static multiple times.
Use a reverse proxy cache like NGINX to improve performance of serving static assets.
- // Serve files from "./public" directory:
- app.Static("/", "./public")
- // Serve files from "./files" directory:
- 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:
- app.Static("/static", "./public")
- // => http://localhost:3000/static/hello.html
- // => http://localhost:3000/static/js/jquery.js
- // => 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.
- // Static represents settings for serving static files
- type Static struct {
- // Transparently compresses responses if set to true
- // This works differently than the github.com/gofiber/compression middleware
- // The server tries minimizing CPU usage by caching compressed files.
- // It adds ".fiber.gz" suffix to the original file name.
- // Optional. Default value false
- Compress bool
- // Enables byte range requests if set to true.
- // Optional. Default value false
- ByteRange bool
- // Enable directory browsing.
- // Optional. Default value false.
- Browse bool
- // Index file for serving a directory.
- // Optional. Default value "index.html".
- Index string
- }
- app.Static("/", "./public", fiber.Static{
- Compress: true,
- ByteRange: true,
- Browse: true,
- Index: "john.html"
- })