2016-10-20 20:30:53 +02:00
|
|
|
+++
|
|
|
|
title = "Static Files"
|
|
|
|
description = "Serving static files in Echo"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
2016-10-20 20:30:53 +02:00
|
|
|
name = "Static Files"
|
|
|
|
parent = "guide"
|
|
|
|
weight = 3
|
|
|
|
+++
|
|
|
|
|
|
|
|
Images, JavaScript, CSS, PDF, Fonts and so on...
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Using `Echo#Static()`
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`Echo#Static(prefix, root string)` registers a new route with path prefix to serve
|
|
|
|
static files from the provided root directory.
|
|
|
|
|
|
|
|
*Usage 1*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e := echo.New()
|
|
|
|
e.Static("/static", "assets")
|
|
|
|
```
|
|
|
|
|
|
|
|
Example above will serve any file from the assets directory for path `/static/*`. For example,
|
|
|
|
a request to `/static/js/main.js` will fetch and serve `assets/js/main.js` file.
|
|
|
|
|
|
|
|
*Usage 2*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e := echo.New()
|
|
|
|
e.Static("/", "assets")
|
|
|
|
```
|
|
|
|
|
|
|
|
Example above will serve any file from the assets directory for path `/*`. For example,
|
|
|
|
a request to `/js/main.js` will fetch and serve `assets/js/main.js` file.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Using `Echo#File()`
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`Echo#File(path, file string)` registers a new route with path to serve a static
|
|
|
|
file.
|
|
|
|
|
|
|
|
*Usage 1*
|
|
|
|
|
|
|
|
Serving an index page from `public/index.html`
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.File("/", "public/index.html")
|
|
|
|
```
|
|
|
|
|
|
|
|
*Usage 2*
|
|
|
|
|
|
|
|
Serving a favicon from `images/favicon.ico`
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.File("/favicon.ico", "images/favicon.ico")
|
|
|
|
```
|