mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-23 08:43:57 +00:00
10 KiB
10 KiB
Fiber是一个基于Express的 Web框架,建立在Fasthttp ( Go 最快的 HTTP引擎)的基础上。皆在简化 零内存分配和提高性能,以便快速开发。
⚡️ 快速入门
package main
import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})
app.Listen(3000)
}
⚙️ 安装
首先, 下载并安装Go。 1.11
或更高。
使用go get
命令完成安装:
go get github.com/gofiber/fiber
🤖 性能
这些测试由TechEmpower和Go Web执行 。如果要查看所有结果,请访问我们的Wiki 。
🎯 特点
💡 哲学
从Node.js切换到Go的新gopher在开始构建Web应用程序或微服务之前正在应对学习过程。 Fiber作为一个Web框架 ,是按照极简主义的思想并遵循UNIX方式创建的 ,因此新的gopher可以以热烈和可信赖的欢迎方式迅速进入Go的世界。
Fiber 受 Internet上最流行的Web框架Expressjs的启发 。我们结合了Express的易用性和Go的原始性能 。如果您曾经在Node.js上实现过Web应用程序( 使用Express.js或类似工具 ),那么许多方法和原理对您来说似乎非常易懂 。
👀 例子
下面列出了一些常见示例。如果您想查看更多代码示例,请访问我们的Recipes存储库或访问我们的API文档 。
Serve static files
func main() {
app := fiber.New()
app.Static("/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Listen(3000)
}
Routing
func main() {
app := fiber.New()
// GET /john
app.Get("/:name", func(c *fiber.Ctx) {
fmt.Printf("Hello %s!", c.Params("name"))
// => Hello john!
})
// GET /john
app.Get("/:name/:age?", func(c *fiber.Ctx) {
fmt.Printf("Name: %s, Age: %s", c.Params("name"), c.Params("age"))
// => Name: john, Age:
})
// GET /api/register
app.Get("/api*", func(c *fiber.Ctx) {
fmt.Printf("/api%s", c.Params("*"))
// => /api/register
})
app.Listen(3000)
}
Middleware & Next
func main() {
app := fiber.New()
// Match any route
app.Use(func(c *fiber.Ctx) {
fmt.Println("First middleware")
c.Next()
})
// Match all routes starting with /api
app.Use("/api", func(c *fiber.Ctx) {
fmt.Println("Second middleware")
c.Next()
})
// POST /api/register
app.Post("/api/register", func(c *fiber.Ctx) {
fmt.Println("Last middleware")
c.Send("Hello, World!")
})
app.Listen(3000)
}
📜 Show more code examples
Custom 404 response
func main() {
app := fiber.New()
app.Static("/public")
app.Get("/demo", func(c *fiber.Ctx) {
c.Send("This is a demo!")
})
app.Post("/register", func(c *fiber.Ctx) {
c.Send("Welcome!")
})
// Last middleware to match anything
app.Use(func(c *fiber.Ctx) {
c.SendStatus(404) // => 404 "Not Found"
})
app.Listen(3000)
}
JSON Response
func main() {
app := fiber.New()
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
// Serialize JSON
app.Get("/json", func(c *fiber.Ctx) {
c.JSON(&User{"John", 20})
// => {"name":"John", "age":20}
})
app.Listen(3000)
}
Recover from panic
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
panic("Something went wrong!")
})
app.Recover(func(c *fiber.Ctx) {
c.Status(500).Send(c.Error())
// => 500 "Something went wrong!"
})
app.Listen(3000)
}
💬 媒体
👍 贡献
如果您要说声谢谢或支持Fiber
的积极发展:
- 将GitHub Star添加到项目中。
- 在Twitter上发布有关项目的推文 。
- 在Medium , Dev.to或个人博客上写评论或教程。
- 帮助我们将此
README
文件和API文档翻译成另一种语言
☕ Supporters

HenrikBinggl |
koddr |
MarvinJWendt |
ToishY |
⭐️ 星星
⚠️ 许可证
Fiber
是根据MIT许可证许可的免费开源软件。