1
0
mirror of https://github.com/labstack/echo.git synced 2026-05-18 10:01:24 +02:00
Files
echo/website/content/middleware/static.md
T
Vishal Rana 17c8b8f2ea logger new api, updated recipes
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-10-23 13:37:04 -07:00

1.6 KiB

+++ title = "Static Middleware" description = "Static middleware for Echo" [menu.side] name = "Static" parent = "middleware" weight = 5 +++

Static Middleware

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

Usage

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

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

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

Configuration

StaticConfig struct {
    // Skipper defines a function to skip middleware.
    Skipper Skipper

    // Prefix to strip from the request URL path. Typically used with `Echo#Group`.
    // Optional. Default value "".
    Prefix string `json:"root"`

    // 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"`
}

Default Configuration

DefaultStaticConfig = StaticConfig{
  Skipper: defaultSkipper,
  Index:   "index.html",
}