Git Product home page Git Product logo

docs's Introduction

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

๐Ÿ“– Getting started

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:

go get -u github.com/gofiber/fiber

Hello, World!

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

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()

  app.Get("/", func(c *fiber.Ctx) {
    c.Send("Hello, World!")
  })

  app.Listen(3000)
}
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).

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

Route definition takes the following structures:

// Function signature
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

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

Parameters

// 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
})

Optional parameter

// GET http://localhost:3000/john

app.Get("/:name?", func(c *fiber.Ctx) {
  if c.Params("name") != "" {
    c.Send("Hello " + c.Params("name"))
    // => Hello john
  } else {
    c.Send("Where is john?")
  }
})

Wildcards

// GET http://localhost:3000/api/user/john

app.Get("/api/*", func(c *fiber.Ctx) {
  c.Send("API path: " + c.Params("*"))
  // => API path: 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(prefix, root string)

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

app := fiber.New()

app.Static("/", "./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

docs's People

Contributors

debabratakarfa avatar dechristopher avatar fenny avatar jozsefsallai avatar kieranholroyd avatar koddr avatar mt-gitlocalize avatar renanbastos93 avatar soonoo avatar thomasvvugt avatar tschaub avatar tteerawat avatar yhg8423 avatar ztmark avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.