fiber/docs/getting_started.md

4.3 KiB
Raw Blame History

accessibility text

Latest Release GoDoc Go Report GitHub license Join the chat at https://gitter.im/FiberGo/community

Getting started

!>IMPORTANT: Always use versioning control using go.mod to avoid breaking API changes!

Fiber is a router framework build on top of FastHTTP, the fastest HTTP package for Go.
This library is inspired by Express, one of the most populair and well known web framework for Nodejs.

Installing

Assuming youve 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!