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.
  1. app.Static(prefix, root string, config ...Static) // => with prefix

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

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

To serve from multiple directories, you can use Static multiple times.

  1. // Serve files from "./public" directory:
  2. app.Static("/", "./public")
  3.  
  4. // Serve files from "./files" directory:
  5. app.Static("/", "./files")
Use a reverse proxy cache like NGINX to improve performance of serving static assets.

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.  
  3. // => http://localhost:3000/static/hello.html
  4. // => http://localhost:3000/static/js/jquery.js
  5. // => 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. // Index file for serving a directory.
  16. // Optional. Default value "index.html".
  17. Index string
  18. }
  1. app.Static("/", "./public", fiber.Static{
  2. Compress: true,
  3. ByteRange: true,
  4. Browse: true,
  5. Index: "john.html"
  6. })