description: An API documentation so you can start building web apps with Fiber.

📖 Getting started

📖 Getting started - 图1 📖 Getting started - 图2 📖 Getting started - 图3 📖 Getting started - 图4 📖 Getting started - 图5 📖 Getting started - 图6

Fiber is an Express inspired web framework build on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.

Installation

First of all, download and install Go. 1.11 or higher is required.

Installation is done using the go get command:

  1. go get -u github.com/gofiber/fiber

Hello, World!

Embedded below is essentially simplest Fiber app, which you can create.

  1. package main
  2. import "github.com/gofiber/fiber/v2"
  3. func main() {
  4. app := fiber.New()
  5. app.Get("/", func(c *fiber.Ctx) {
  6. c.Send("Hello, World!")
  7. })
  8. app.Listen(":3000")
  9. }
  1. go run server.go

Browse to http://localhost:3000 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, PUT, POST and so on).

Each route can have multiple handler functions, that are executed when the route is matched.

Route definition takes the following structures:

  1. // Function signature
  2. 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 is a virtual path on the server.
  • func(*fiber.Ctx) is a callback function containing the Context executed when the route is matched.

Simple route

  1. // Respond with "Hello, World!" on root path, "/"
  2. app.Get("/", func(c *fiber.Ctx) {
  3. c.Send("Hello, World!")
  4. })

Parameters

  1. // GET http://localhost:8080/hello%20world
  2. app.Get("/:value", func(c *fiber.Ctx) {
  3. c.Send("Get request with value: " + c.Params("value"))
  4. // => Get request with value: hello world
  5. })

Optional parameter

  1. // GET http://localhost:3000/john
  2. app.Get("/:name?", func(c *fiber.Ctx) {
  3. if c.Params("name") != "" {
  4. c.Send("Hello " + c.Params("name"))
  5. // => Hello john
  6. } else {
  7. c.Send("Where is john?")
  8. }
  9. })

Wildcards

  1. // GET http://localhost:3000/api/user/john
  2. app.Get("/api/*", func(c *fiber.Ctx) {
  3. c.Send("API path: " + c.Params("*"))
  4. // => API path: user/john
  5. })

Static files

To serve static files such as images, CSS and JavaScript files, replace your function handler with a file or directory string.

Function signature:

  1. app.Static(prefix, root string)

Use the following code to serve files in a directory named ./public:

  1. app := fiber.New()
  2. app.Static("/", "./public")
  3. app.Listen(":8080")

Now, you can load the files that are in the ./public directory:

  1. http://localhost:8080/hello.html
  2. http://localhost:8080/js/jquery.js
  3. http://localhost:8080/css/style.css