static

Static

原文:https://echo.labstack.com/docs/middleware/static

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

Usage

1
2
e := echo.New()
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
2
3
4
5
e := echo.New()
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
  Root:   "static",
  Browse: true,
}))

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
2
3
group := root.Group("somepath")
group.Use(middleware.Static(filepath.Join("filesystempath")))
// When an incoming request comes for `/somepath` the actual filesystem request goes to `filesystempath/somepath` instead of only `filesystempath`. 

TIP

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

Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
StaticConfig struct {
  // Skipper defines a function to skip middleware.
  Skipper Skipper

  // Root directory from where the static content is served.
  // Required.
  Root string `json:"root"`

  // Index file for serving a directory.
  // Optional. Default value "index.html".
  Index string `json:"index"`

  // Enable HTML5 mode by forwarding all not-found requests to root so that
  // SPA (single-page application) can handle the routing.
  // Optional. Default value false.
  HTML5 bool `json:"html5"`

  // Enable directory browsing.
  // Optional. Default value false.
  Browse bool `json:"browse"`
  
  // Enable ignoring of the base of the URL path.
  // Example: when assigning a static middleware to a non root path group,
  // the filesystem path is not doubled
  // Optional. Default value false.
  IgnoreBase bool `yaml:"ignoreBase"`
}

Default Configuration

1
2
3
4
DefaultStaticConfig = StaticConfig{
  Skipper: DefaultSkipper,
  Index:   "index.html",
}
最后修改 October 10, 2024: 更新 (a4b8f85)