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.

Default behavior when using with non root URL paths is to append the URL path to the filesystem path.

Example

  1. group := root.Group("somepath")
  2. group.Use(middleware.Static(filepath.Join("filesystempath")))
  3. // When an incoming request comes for `/somepath` the actual filesystem request goes to `filesystempath/somepath` instead of only `filesystempath`.

Static - 图1tip

To turn off this behavior set the IgnoreBase config param to true.

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. // Enable ignoring of the base of the URL path.
  18. // Example: when assigning a static middleware to a non root path group,
  19. // the filesystem path is not doubled
  20. // Optional. Default value false.
  21. IgnoreBase bool `yaml:"ignoreBase"`
  22. }

Default Configuration

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