Fiber is An Express.js Inspired Web Framework Written in Go

Fiber

Fiber is an Express.js styled HTTP web framework implementation running on Fasthttp, the fastest HTTP engine for Go (Golang). The package make use of similar framework convention as they are in Express.

People switching from Node.js 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.

API Documentation

We created an extended API documentation (including examples), Visit fiber.wiki.

Benchmark

Benchmark

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
  • Extended documentation

Installing

Assuming you’ve already installed Go 1.11+

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:

// server.go

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.Send("Hello, World!")
  })

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

Go to console and run:

go run server.go

And now, 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() {
  // Create new Fiber instance
  app := fiber.New()

  // Serve all static files on ./public folder
  app.Static("./public")

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

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

http://localhost:8080/hello.html
http://localhost:8080/js/script.js
http://localhost:8080/css/style.css

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() {
  // Create new Fiber instance
  app := fiber.New()

  // Define all used middlewares in Use()

  app.Use(func(c *fiber.Ctx) {
    c.Write("Match anything!\n")
    c.Next()
  })

  app.Use("/api", func(c *fiber.Ctx) {
    c.Write("Match starting with /api\n")
    c.Next()
  })

  app.Get("/api/user", func(c *fiber.Ctx) {
    c.Write("Match exact path /api/user\n")
  })

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

Stars over time

Stars over time

#GO #html #golang #nodejs

Fiber is An Express.js Inspired Web Framework Written in Go
41.75 GEEK