pull/6/head
Fenny 2020-01-20 07:39:51 +01:00
parent 2730e0c2ff
commit fe35768f4e
5 changed files with 43 additions and 25 deletions

View File

@ -121,6 +121,42 @@ app.All(...)
app.Use(...)
```
#### Static
To serve static files such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.
```go
// Function signature
app.Static(root string)
app.Static(prefix, root string)
```
For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:
```go
app.Static("./public")
```
Now, you can load the files that are in the public directory:
```shell
http://localhost:8080/hello.html
http://localhost:8080/js/jquery.js
http://localhost:8080/css/style.css
```
To use multiple static assets directories, call the Static function multiple times:
```go
app.Static("./public")
app.Static("./files")
```
?>For best results, use a reverse proxy cache like [NGINX](https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/) to improve performance of serving static assets.
To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:
```go
app.Static("/static", "./public")
```
Now, you can load the files that are in the public directory from the /static path prefix.
```shell
http://localhost:8080/static/hello.html
http://localhost:8080/static/js/jquery.js
http://localhost:8080/static/css/style.css
```
#### Listen
Binds and listens for connections on the specified address. This can be a **INT** for port or **STRING** for address. To enable **TLS/HTTPS** you can append your **cert** and **key** path.
```go

View File

@ -836,7 +836,7 @@ app.Get("/not-found", func(c *fiber.Ctx) {
```
#### SendStatus
Sets the status code, but also the correct status message in the body if the response body is still empty.
Sets the status code, but also the correct status message in the body if the response body is still empty. You can find all status codes and messages in [status.go](https://github.com/gofiber/fiber/blob/master/status.go)
```go
// Function signature
c.SendStatus(status int)

View File

@ -29,9 +29,11 @@ import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
app.Listen(8080)
}
```
@ -106,30 +108,9 @@ app.Static("./public")
```
Now, you can load the files that are in the public directory:
```shell
http://localhost:8080/images/kitten.jpg
http://localhost:8080/css/style.css
http://localhost:8080/js/app.js
http://localhost:8080/images/bg.png
http://localhost:8080/hello.html
```
To use multiple static assets directories, call the Static function multiple times:
```go
app.Static("./public")
app.Static("./files")
```
?>For best results, use a reverse proxy cache like [NGINX](https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/) to improve performance of serving static assets.
To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:
```go
app.Static("/static", "./public")
```
Now, you can load the files that are in the public directory from the /static path prefix.
```shell
http://localhost:8080/static/images/kitten.jpg
http://localhost:8080/static/css/style.css
http://localhost:8080/static/js/app.js
http://localhost:8080/static/images/bg.png
http://localhost:8080/static/hello.html
http://localhost:8080/js/jquery.js
http://localhost:8080/css/style.css
```
*Caught a mistake? [Edit this page on GitHub!](https://github.com/gofiber/fiber/blob/master/docs/getting_started.md)*

View File

@ -12,7 +12,7 @@ import (
"strings"
)
// Static ...
// Static https://gofiber.github.io/fiber/#/application?id=static
func (r *Fiber) Static(args ...string) {
prefix := "/"
root := "./"

View File

@ -10,6 +10,7 @@ package fiber
// Credits @valyala
// https://github.com/valyala/fasthttp/blob/master/status.go
// statusMessages https://gofiber.github.io/fiber/#/context?id=sendstatus
var statusMessages = map[int]string{
100: "Continue",
101: "Switching Protocols",