受 Express 启发的,用 Go 编写的 Web 框架。
Fiber 是一个建立在 Fasthttp 之上的 Go Web 框架,Fasthttp 是 Go 中最快的 HTTP 引擎。它旨在简化快速开发,同时兼顾零内存分配和性能。
package main
import (
"log"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func (c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
log.Fatal(app.Listen(":3000"))
}
Hello, World!
稳健的路由
为你的应用程序设置路由从未如此简单!类似 Express 的路由定义易于理解和使用。
app.Get("/", func (c *fiber.Ctx) error {
return c.SendString("GET request")
})
app.Get("/:param", func (c *fiber.Ctx) error {
return c.SendString("param: " + c.Params("param"))
})
app.Post("/", func (c *fiber.Ctx) error {
return c.SendString("POST request")
})
提供静态文件
通过定义静态路由轻松提供你的静态 HTML、CSS 和 JavaScript 文件。你还可以将多个目录的内容提供到同一个路由上!
app.Static("/", "./public")
// => http://localhost:3000/hello.html
// => http://localhost:3000/js/jquery.js
// => http://localhost:3000/css/style.css
// serve from multiple directories
app.Static("/", "./files")
极端性能
由于 Fiber 建立在 Fasthttp 之上,你的应用程序将享受到无与伦比的性能!不相信我们?这里有一个基准测试,证明了 Fiber 与其他框架相比的优势
API 就绪
你在构建 API 服务器吗?我们帮你搞定!Fiber 是用 Go 构建 REST API 的完美选择。接收和发送数据快速且容易!
app.Get("/api/posts", func (c *fiber.Ctx) error {
posts := getPosts() // your logic
if len(posts) == 0 {
return c.Status(404).JSON(&fiber.Map{
"success": false,
"error": "There are no posts!",
})
}
return c.JSON(&fiber.Map{
"success": true,
"posts": posts,
})
})
灵活的中间件支持
从大量已存在的中间件中进行选择,或创建你自己的中间件!在请求到达控制器之前,使用它们来验证和处理应用程序中的某些请求。
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
func main() {
app := fiber.New()
app.Use(cors.New())
app.Use(func (c *fiber.Ctx) error {
if c.Is("json") {
return c.Next()
}
return c.SendString("Only JSON allowed!")
})
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"message": "Hello World",
})
})
log.Fatal(app.Listen(":3000"))
}
低内存占用
Fiber 的低内存占用使你能够实现功能,而不用过多地担心应用程序将使用多少内存。这使你能够专注于应用程序及其业务逻辑,而不是技术细节。
快速编程
立即将你的想法变为现实!得益于设计精良且易于学习的 API,你可以以创纪录的速度开发应用程序(特别是如果你来自 Express.js 背景)。
模板引擎
想在你的 Fiber 应用程序中使用不同的模板引擎吗?别担心!得益于模板中间件,Fiber 支持多种模板引擎,例如 Handlebars 和 Pug。
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
)
func main() {
app := fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
app.Get("/", func(c *fiber.Ctx) error {
return c.Render("index", fiber.Map{
"Title": "Hello, World!",
})
})
log.Fatal(app.Listen(":3000"))
}
WebSocket 支持
在你的 Fiber 应用中使用强大的 WebSocket!构建快速交互式用户体验,性能和可扩展性有保障。
app.Get("/ws", websocket.New(func(c *websocket.Conn) {
// Websocket logic
for {
mtype, msg, err := c.ReadMessage()
if err != nil {
break
}
log.Printf("Read: %s", msg)
err = c.WriteMessage(mtype, msg)
if err != nil {
break
}
}
log.Println("Error:", err)
}))
速率限制器
使用 Fiber,限制对公共 API 和端点的重复请求非常简单。不再有滥用请求!
package main
import (
"log"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/limiter"
)
func main() {
app := fiber.New()
// 3 requests per 10 seconds max
app.Use(limiter.New(limiter.Config{
Expiration: 10 * time.Second,
Max: 3,
}))
// ...
log.Fatal(app.Listen(":3000"))
}
帮助和支持
加入一个热衷于 Fiber 的开发者社区。在我们的 Discord 服务器中提问、分享你的创作,尽情享受。
媒体
📖 Go Fiber by Examples: Working with middlewares and boilerplates
by Vic Shóstak on september 13, 2021 via dev.to
📖 Go Fiber by Examples: Testing the application
by Vic Shóstak on august 30, 2021 via dev.to
📖 Go Fiber by Examples: Delving into built-in functions
by Vic Shóstak on august 24, 2021 via dev.to
📖 Go Fiber by Examples: How can the Fiber Web Framework be useful?
by Vic Shóstak on august 16, 2021 via dev.to
📖 Build a RESTful API on Go: Fiber, PostgreSQL, JWT and Swagger docs in isolated Docker containers
by Vic Shóstak on march 22, 2021 via dev.to
Getting started with Fiber ⚡
by Fenny 🔥 on june 10, 2020 via dev.to
Building an Express-style API in Go with Fiber
by Alexander Nnakwue on june 10, 2020 via logrocket.com
Fiber v1.9.6 🔥 How to improve performance by 817% and stay fast, flexible and friendly?
by Vic Shóstak on May 12, 2020 via dev.to
🌎 Create a travel list app with Go, Fiber, Angular, MongoDB and Google Cloud Secret Manager
by Wei Lun on April 25, 2020 via yongweilun.me
Building a Basic REST API in Go using Fiber
by Elliot Forbes on April 23, 2020 via tutorialedge.net
Creating Fast APIs In Go Using Fiber
by József Sallai on April 7, 2020 via dev.to
Is switching from Express to Fiber worth it? 🤔
by Vic Shóstak on April 1, 2020 via dev.to
🚀 Fiber v1.8. What's new, updated and re-thinked?
by Vic Shóstak on March 3, 2020 via dev.to
Fiber released v1.7! 🎉 What's new and is it still fast, flexible and friendly?
by Vic Shóstak on February 21, 2020 via dev.to
Welcome to Fiber — an Express.js styled web framework written in Go with ❤️
by Vic Shóstak on February 3, 2020 via dev.to