1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 12:23:38 +00:00
2020-01-17 21:25:11 +01:00
2020-01-17 21:25:11 +01:00
2020-01-16 05:58:23 +01:00
2020-01-16 13:34:53 +01:00
fix
2020-01-16 06:02:00 +01:00
2020-01-17 21:25:11 +01:00
2020-01-17 21:25:11 +01:00
2020-01-16 10:26:35 +01:00
2020-01-16 11:03:04 +01:00
2020-01-17 21:25:11 +01:00
2020-01-16 05:58:23 +01:00
2020-01-16 05:58:23 +01:00

Fiber

Fiber is an Express styled HTTP framework implementation running on Fasthttp, the fastest HTTP engine for Go. The package make use of similar framework convention as they are in express. People switching from Node to Go often end up in a bad learning curve to start building their webapps, this project is meant to ease things up for fast development, but with zero memory allocation and performance in mind. See API Documentation

Features

  • Optimized for speed and low memory usage.
  • Rapid Server-Side Programming
  • Easy routing with parameters
  • Static files with custom prefix
  • Middleware with Next support
  • Express API endpoints
  • API Documentation

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("/:name", func(c *fiber.Ctx) {
    c.Send("Hello, " + c.Params("name") + "!")
  })
  app.Listen(8080)
}
$ go run server.go

Browse to http://localhost:8080 and you should see Hello, World! on the page.

Static files

To serve static files, use the Static method.

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  app.Static("./public")
  app.Listen(8080)
}

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

http://localhost:8080/images/gopher.png
http://localhost:8080/css/style.css
http://localhost:8080/js/jquery.js
http://localhost:8080/hello.html

Middleware

Middleware has never been so easy, just like express you call the Next() matching route function!

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  app.Get("/api*", func(c *fiber.Ctx) {
    c.Locals("auth", "admin")
    c.Next()
  })
  app.Get("/api/back-end/:action?", func(c *fiber.Ctx) {
    if c.Locals("auth") != "admin" {
      c.Status(403).Send("Forbidden")
      return
    }
    c.Send("Hello, Admin!")
  })
  app.Listen(8080)
}

API Documentation

We created an extended API documentation including examples, click here

License

gofiber/fiber is free and open-source software licensed under the MIT License.

Caught a mistake? Edit this page on GitHub!

Languages
Go 99.9%
Makefile 0.1%