4.3 KiB
Getting started
Fiber is an Express.js styled HTTP web framework implementation running on Fasthttp, the fastest HTTP engine for Go (Golang). The package make use of similar framework convention as they are in Express.
People switching from Node.js to Go often end up in a bad learning curve to start building their webapps, this project is meant to ease things up for fast development, but with zero memory allocation and performance in mind.
Installing
Assuming you’ve already installed Go, install the Fiber package by calling the following command:
go get -u github.com/gofiber/fiber
Hello world
Embedded below is essentially the simplest Fiber app you can create.
create server.go
package main
import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
app.Listen(8080)
}
go run server.go
Browse to http://localhost:8080 and you should see Hello, World! on the page.
Basic routing
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on).
Each route can have one handler function, that is executed when the route is matched.
Route definition takes the following structures:
// Function signature
app.Method(func(*fiber.Ctx))
app.Method(path string, func(*fiber.Ctx))
- app is an instance of Fiber.
- Method is an HTTP request method, in capitalization: Get, Put, Post etc
- path string is a path on the server.
- func(*fiber.Ctx) is a function containing the Context executed when the route is matched.
This tutorial assumes that an instance of fiber named app is created and the server is running. If you are not familiar with creating an app and starting it, see the Hello world example.
The following examples illustrate defining simple routes.
// Respond with Hello, World! on the homepage:
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
// Parameter
// http://localhost:8080/hello%20world
app.Post("/:value", func(c *fiber.Ctx) {
c.Send("Post request with value: " + c.Params("value"))
// => Post request with value: hello world
})
// Optional parameter
// http://localhost:8080/hello%20world
app.Get("/:value?", func(c *fiber.Ctx) {
if c.Params("value") != "" {
c.Send("Get request with value: " + c.Params("Value"))
return // => Post request with value: hello world
}
c.Send("Get request without value")
})
// Wildcard
// http://localhost:8080/api/user/john
app.Get("/api/*", func(c *fiber.Ctx) {
c.Send("API path with wildcard: " + c.Params("*"))
// => API path with wildcard: user/john
})
Static files
To serve static files such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.
// 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:
app.Static("./public")
Now, you can load the files that are in the public directory:
http://localhost:8080/hello.html
http://localhost:8080/js/jquery.js
http://localhost:8080/css/style.css
Caught a mistake? Edit this page on GitHub!