🚀 App
By default, Static will serve files in response to a request on a directory.Use the following code to serve files in a directory named ./public
app.Static("/", "./public")
// => http://localhost:3000/hello.html
// => http://localhost:3000/js/jquery.js
// => http://localhost:3000/css/style.css
// Serve files from multiple directories
app.Static("/", "./public")
// Serve files from "./files" directory:
app.Static("/", "./files")
You can use any virtual path prefix (where the path does not actually exist in the file system) for files that are served by the Static method, specify a prefix path for the static directory, as shown below:
app.Static("/static", "./public")
// => http://localhost:3000/static/hello.html
// => http://localhost:3000/static/js/jquery.js
// => http://localhost:3000/static/css/style.css
If you want to have a little bit more control regarding the settings for serving static files. You could use the fiber.Static
struct to enable specific settings.
// Static represents settings for serving static files
type Static struct {
// Transparently compresses responses if set to true
// This works differently than the github.com/gofiber/compression middleware
// The server tries minimizing CPU usage by caching compressed files.
// It adds ".fiber.gz" suffix to the original file name.
// Optional. Default value false
Compress bool
// Enables byte-range requests if set to true.
// Optional. Default value false
ByteRange bool
// Enable directory browsing.
// Optional. Default value false.
Browse bool
// File to serve when requesting a directory path.
// Optional. Default value "index.html".
Index string
}
// Custom config
app.Static("/", "./public", fiber.Static{
Compress: true,
ByteRange: true,
Browse: true,
Index: "john.html"
})
Route Handlers
Registeres a route bound to a specific .
// HTTP methods
func (app *App) Head(path string, handlers ...Handler) Router
func (app *App) Post(path string, handlers ...Handler) Router
func (app *App) Put(path string, handlers ...Handler) Router
func (app *App) Delete(path string, handlers ...Handler) Router
func (app *App) Connect(path string, handlers ...Handler) Router
func (app *App) Options(path string, handlers ...Handler) Router
func (app *App) Patch(path string, handlers ...Handler) Router
// Add allows you to specifiy a method as value
func (app *App) Add(method, path string, handlers ...Handler) Router
// All will register the route on all HTTP methods
// Almost the same as app.Use but not bound to prefixes
func (app *App) All(path string, handlers ...Handler) Router
// Match any request
app.Use(func(c *fiber.Ctx) error {
return c.Next()
})
// Match request starting with /api
app.Use("/api", func(c *fiber.Ctx) error {
return c.Next()
})
// Attach multiple handlers
app.Use("/api",func(c *fiber.Ctx) error {
c.Set("X-Custom-Header", random.String(32))
return c.Next()
}, func(c *fiber.Ctx) error {
return c.Next()
})
Group
You can group routes by creating a *Group
struct.
Signature
func (app *App) Group(prefix string, handlers ...Handler) Router
Example
func main() {
app := fiber.New()
api := app.Group("/api", handler) // /api
v1 := api.Group("/v1", handler) // /api/v1
v1.Get("/list", handler) // /api/v1/list
v1.Get("/user", handler) // /api/v1/user
v2 := api.Group("/v2", handler) // /api/v2
v2.Get("/list", handler) // /api/v2/list
v2.Get("/user", handler) // /api/v2/user
app.Listen(3000)
}
This method returns the original router stack
func (app *App) Stack() [][]*Route
var handler = func(c *fiber.Ctx) {}
func main() {
app := fiber.New()
app.Get("/john", handler)
app.Post("/register", handler)
data, _ := json.MarshalIndent(app.Stack(), "", " ")
fmt.Println(string(data))
}
[
[
{
"method": "GET",
"path": "/john/:age",
"params": [
"age"
]
}
],
[
{
"method": "HEAD",
"path": "/john/:age",
"params": [
"age"
]
}
],
[
{
"method": "POST",
"path": "/register",
"params": null
}
]
]
Config
Handler
Handler returns the server handler that can be used to serve custom *fasthttp.RequestCtx requests.
func (app *App) Handler() fasthttp.RequestHandler
Listen serves HTTP requests from the given address.
func (app *App) Listen(addr string) error
// Listen on port :8080
app.Listen(":8080")
// Custom host
app.Listen("127.0.0.1:8080")
Listener
You can pass your own net.Listener
using the Listener
method. This method can be used to enable TLS/HTTPS.
func (app *App) Listener(ln net.Listener) error
ln, _ := net.Listen("tcp", ":3000")
cer, _:= tls.LoadX509KeyPair("server.crt", "server.key")
ln = tls.NewListener(ln, &tls.Config{Certificates: []tls.Certificate{cer}})
Test
Testing your application is done with the Test method. Use this method for creating _test.go
files or when you need to debug your routing logic. The default timeout is 200ms
if you want to disable a timeout altogether, pass -1
as a second argument.