Skip to content
forked from gofiber/docs

πŸ“š Documentation for πŸš€ Fiber

Notifications You must be signed in to change notification settings

dechristopher/docs

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

description
A hosted documentation so you can start building web apps with Fiber.

πŸ“– Getting started

Fiber is an Expressjs 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.

Installing

First of all, download and install Go.

{% hint style="success" %} Go 1.11 (with enabled Go Modules) or higher is required. {% endhint %}

Installation is done using the go get command:

go get -u github.com/gofiber/fiber

Hello, World!

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

touch server.go
package main

import "github.com/gofiber/fiber"

func main() {
  // Create new Fiber instance:
  app := fiber.New()

  // Create route on root path, "/":
  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Hello, World!")
    // => "Hello, World!"
  })

  // Start server on "localhost" with port "8080":
  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, PUT, POST and so on).

{% hint style="info" %} Each route can have one handler function, that is executed when the route is matched. {% endhint %}

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

Simple route

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

Route with parameter

// GET http://localhost:8080/hello%20world

app.Get("/:value", func(c *fiber.Ctx) {
  c.Send("Get request with value: " + c.Params("value"))
  // => Get request with value: hello world
})

Route with optional parameter

// GET 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"))
    // => Get request with value: hello world
    return
  }

  c.Send("Get request without value")
})

Route with wildcard

// GET 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 and JavaScript files, replace your function handler with a file or directory string.

Function signature:

app.Static(root string)         // => without prefix
app.Static(prefix, root string) // => with prefix

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

app := fiber.New()

app.Static("./public") // => Serve all files into ./public

app.Listen(8080)

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

About

πŸ“š Documentation for πŸš€ Fiber

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CSS 100.0%