Skip to content

Commit

Permalink
add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed May 28, 2024
1 parent 502fbc7 commit 740c15c
Showing 1 changed file with 40 additions and 67 deletions.
107 changes: 40 additions & 67 deletions docs/middleware/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,18 @@ func New(root string, cfg ...Config) fiber.Handler

## Examples

### Serving files from a directory

Import the middleware package that is part of the [Fiber](https://github.com/gofiber/fiber) web framework
```go
package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
import(
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
)
```

func main() {
app := fiber.New()

app.Get("/*", static.New("./public"))

app.Listen(":3000")
}
### Serving files from a directory

```go
app.Get("/*", static.New("./public"))
```
<details>
Expand All @@ -50,20 +45,7 @@ curl http://localhost:3000/css/style.css
### Serving files from a directory with Use
```go
package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
)

func main() {
app := fiber.New()

app.Use("/", static.New("./public"))

app.Listen(":3000")
}
app.Use("/", static.New("./public"))
```
<details>
Expand All @@ -79,20 +61,7 @@ curl http://localhost:3000/css/style.css
### Serving a file
```go
package main

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
)

func main() {
app := fiber.New()

app.Use("/static", static.New("./public/hello.html"))

app.Listen(":3000")
}
app.Use("/static", static.New("./public/hello.html"))
```
<details>
Expand All @@ -108,23 +77,32 @@ curl http://localhost:3000/static/john/doee # will show hello.html
### Serving files using os.DirFS
```go
package main
app.Get("/files*", static.New("", static.Config{
FS: os.DirFS("files"),
Browse: true,
}))
```
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
)
<details>
<summary>Test</summary>
func main() {
app := fiber.New()

app.Get("/files*", static.New("", static.Config{
FS: os.DirFS("files"),
```sh
curl http://localhost:3000/files/css/style.css
curl http://localhost:3000/files/index.html
```
</details>
### Serving files using embed.FS
```go
//go:embed path/to/files
var myfiles embed.FS
app.Get("/files*", static.New("", static.Config{
FS: myfiles,
Browse: true,
}))

app.Listen(":3000")
}
}))
```
<details>
Expand All @@ -140,18 +118,13 @@ curl http://localhost:3000/files/index.html
### SPA (Single Page Application)
```go
func main() {
app := fiber.New()
app.Use("/web", static.New("", static.Config{
FS: os.DirFS("dist"),
}))
app.Use("/web", static.New("", static.Config{
FS: os.DirFS("dist"),
}))
app.Get("/web*", func(c fiber.Ctx) error {
return c.SendFile("dist/index.html")
})

app.Listen(":3000")
}
app.Get("/web*", func(c fiber.Ctx) error {
return c.SendFile("dist/index.html")
})
```
<details>
Expand Down

0 comments on commit 740c15c

Please sign in to comment.