Skip to content

Latest commit

 

History

History
210 lines (147 loc) · 6.9 KB

README_NEW.md

File metadata and controls

210 lines (147 loc) · 6.9 KB

Fiber logo

🚀 Fiber ru ch

GitHub license Join the chat at https://gitter.im/gofiber/community

Fiber — is an Express.js inspired web framework build on Fasthttp for Go. Designed to ease things up for fast development with zero memory allocation and performance in mind.

⚡️ Quick start

package main

import "github.com/gofiber/fiber"

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

  // Create new route with GET method
  app.Get("/", func(c *fiber.Ctx) {
    c.Write("Hello, World!")
  })

  // Start server on http://localhost:3000
  app.Listen(3000)
}

⚙️ Installation

First of all, download and install Go.

Go 1.11 (with enabled Go Modules) or higher is required.

Installation is done using the go get command:

go get github.com/gofiber/fiber

🤖 Benchmarks

These tests are performed by TechEmpower and Go Web. If you want to see all results, please visit our Wiki.

🎯 Main features

💡 Philosophy

People switching from Node.js to Go having a heard time on how to start building their web applications or microservices. Fiber, as a web framework, was created with the idea of minimalism and follow UNIX way, so that new gophers can quickly enter the world of Go, but with a warm welcome.

Fiber is inspired by the Express framework, the most popular web framework on Internet. We combined the ease of Express and raw performance of Go. If you have ever implemented a web application on Node.js (using Express.js or similar), then many methods and principles will seem very common to you.

👀 Examples

Listed below are some of the common examples. If you want to see more code examples, please visit Fiber community Cookbook repository or API documentation.

Static files

// ...
app := fiber.New()

// Without prefix
app.Static("./public")

// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css

// With prefix
app.Static("/your-prefix", "./public")

// => http://localhost:3000/your-prefix/js/script.js
// => http://localhost:3000/your-prefix/css/style.css

app.Listen(3000)

Routing

// ...
app := fiber.New()

// URL with param
app.Get("/:name", func(c *fiber.Ctx) {
  c.Send("Hello, " + c.Params("name"))
})

// URL optional param
app.Get("/:name/:lastname?", func(c *fiber.Ctx) {
  c.Send("Hello, " + c.Params("name") + " " + c.Params("lastname"))
})

// URL with wildcard
app.Get("/api*", func(c *fiber.Ctx) {
  c.Send("/api" + c.Params("*"))
})

app.Listen(3000)

Middleware

// ...
app := fiber.New()

// Match any post route
app.Post(func(c *fiber.Ctx) {
  user, pass, ok := c.BasicAuth()
  if !ok || user != "john" || pass != "doe" {
    c.Status(403).Send("Sorry John")
    return
  }
  c.Next()
})

// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
  c.Set("Access-Control-Allow-Origin", "*")
  c.Set("Access-Control-Allow-Headers", "X-Requested-With")
  c.Next()
})

// Optional param
app.Post("/api/register", func(c *fiber.Ctx) {
  username := c.Body("username")
  password := c.Body("password")
  // ..
})

app.Listen(3000)

404 Handling

// ...
app := fiber.New()

// ... app routes here

// The last route
app.Use(func (c *fiber.Ctx) {
  c.SendStatus(404)
})

app.Listen(3000)

JSON Response

// ...
app := fiber.New()

// Data structure
type Data struct {
  Name string `json:"name"`
  Age  int    `json:"age"`
}

// Return JSON data
app.Get("/json", func (c *fiber.Ctx) {
  c.JSON(&Data{
    Name: "John",
    Age:  20,
  })
})

app.Listen(3000)

💬 What people say about Fiber?

👍 Project assistance

If you want to say thank you or/and support active development gofiber/fiber:

  1. Add a GitHub Star to project.
  2. Tweet about project on your Twitter.
  3. Write review and usage articles on Medium, Dev.to and personal blogs.
  4. Help us to translate this README and API Docs to another language.

Thanks for your support! 😘 Together, we make Fiber.

⭐️ Stars over time

Stars over time

⚠️ License

Fiber is free and open-source software licensed under the MIT License.