Static

Static middleware can be used to serve static files from the provided root directory.

Usage

  1. e := echo.New()
  2. e.Use(middleware.Static("/static"))

This serves static files from static directory. For example, a request to /js/main.js
will fetch and serve static/js/main.js file.

Custom Configuration

Usage

  1. e := echo.New()
  2. e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
  3. Root: "static",
  4. Browse: true,
  5. }))

This serves static files from static directory and enables directory browsing.

Configuration

  1. StaticConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // Root directory from where the static content is served.
  5. // Required.
  6. Root string `json:"root"`
  7. // Index file for serving a directory.
  8. // Optional. Default value "index.html".
  9. Index string `json:"index"`
  10. // Enable HTML5 mode by forwarding all not-found requests to root so that
  11. // SPA (single-page application) can handle the routing.
  12. // Optional. Default value false.
  13. HTML5 bool `json:"html5"`
  14. // Enable directory browsing.
  15. // Optional. Default value false.
  16. Browse bool `json:"browse"`
  17. }

Default Configuration

  1. DefaultStaticConfig = StaticConfig{
  2. Skipper: DefaultSkipper,
  3. Index: "index.html",
  4. }