Fiber is a Go web framework inspired by Express.js, built on top of Fasthttp — the fastest HTTP engine in Go. If you're coming from Node.js/Express, Fiber's API will feel immediately familiar while delivering Go-level performance. This guide covers building, structuring, and deploying Fiber applications on a VPS.
Project Setup
# Initialize Go module
mkdir fiber-app && cd fiber-app
go mod init github.com/yourname/fiber-app
# Install Fiber
go get github.com/gofiber/fiber/v2
go get github.com/gofiber/fiber/v2/middleware/logger
go get github.com/gofiber/fiber/v2/middleware/cors
go get github.com/gofiber/fiber/v2/middleware/recover
go get github.com/gofiber/fiber/v2/middleware/compress
Application Code
// main.go
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/compress"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
func main() {
app := fiber.New(fiber.Config{
Prefork: false, // Enable for multi-core (like cluster)
ServerHeader: "Fiber",
StrictRouting: false,
CaseSensitive: false,
BodyLimit: 4 * 1024 * 1024, // 4MB
ReadTimeout: 5 * 1000,
WriteTimeout: 10 * 1000,
IdleTimeout: 120 * 1000,
})
// Middleware
app.Use(recover.New())
app.Use(logger.New())
app.Use(cors.New())
app.Use(compress.New())
// Routes
app.Get("/health", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"status": "healthy"})
})
api := app.Group("/api")
api.Get("/users", getUsers)
api.Get("/users/:id", getUser)
api.Post("/users", createUser)
api.Put("/users/:id", updateUser)
api.Delete("/users/:id", deleteUser)
// Static files
app.Static("/", "./public")
// Graceful shutdown
go func() {
if err := app.Listen(getAddr()); err != nil {
log.Fatalf("Server error: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)