3.8 KiB
Fiber

Fiber is an Express style 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 Nodejs 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.
Features
- Optimized for speed and low memory usage.
- Rapid Server-Side Programming
- Easy routing with parameters
- Static files with custom prefix
- Middleware support
- Supports Express API endpoints
- Extended API Documentation
API Documentation
Installing
Assuming you’ve 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("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
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 such as images, CSS files, and JavaScript files, replace your function handler with a file or directory string.
// Function signature
app.Static(root string)
app.Static(prefix, root string)
For example, use the following code to serve images, CSS files, and JavaScript files in a directory named public:
app.Static("./public")
Now, you can load the files that are in the public directory:
http://localhost:8080/images/kitten.jpg
http://localhost:8080/css/style.css
http://localhost:8080/js/app.js
http://localhost:8080/images/bg.png
http://localhost:8080/hello.html
To use multiple static assets directories, call the express.static middleware function multiple times:
app.Static("./public")
app.Static("./files")
?>For best results, use a reverse proxy cache like NGINX to improve performance of serving static assets.
To create a virtual path prefix (where the path does not actually exist in the file system) for files that are served by the express.static function, specify a mount path for the static directory, as shown below:
app.Static("/static", "./public")
Now, you can load the files that are in the public directory from the /static path prefix.
http://localhost:8080/static/images/kitten.jpg
http://localhost:8080/static/css/style.css
http://localhost:8080/static/js/app.js
http://localhost:8080/static/images/bg.png
http://localhost:8080/static/hello.html
Caught a mistake? Edit this page on GitHub!