1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 19:04:05 +00:00
fiber/.github/README_zh-CN.md
2020-02-11 04:06:43 +01:00

8.1 KiB
Raw Blame History

Fiber

Fiber是一个基于Expressjs的 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

🤖 性能

这些测试由TechEmpowerGo 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文档

静态文件

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.Listen(3000)
}

路由

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)
}

中间件

func main() {
  app := fiber.New()

  // Match any post route
  app.Post(func(c *fiber.Ctx) {
    user, pass, ok := c.BasicAuth()
    if !ok || user != "john" || pass != "doe" {
      c.Status(403).Send("Sorry John")
      return
    }
    c.Next()
  })

  // Match all routes starting with /api
  app.Use("/api", func(c *fiber.Ctx) {
    c.Set("Access-Control-Allow-Origin", "*")
    c.Set("Access-Control-Allow-Headers", "X-Requested-With")
    c.Next()
  })

  // Optional param
  app.Post("/api/register", func(c *fiber.Ctx) {
    username := c.Body("username")
    password := c.Body("password")
    // ..
  })

  app.Listen(3000)
}

404处理

func main() {
  app := fiber.New()

  // Serve static files from "public" directory
  app.Static("./public")

  // Last middleware
  app.Use(func (c *fiber.Ctx) {
    c.SendStatus(404) // => 404 "Not Found"
  })

  app.Listen(3000)
}

JSON响应

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})
  })

  app.Listen(3000)
}

💬 媒体

👍 贡献

如果您要说声谢谢或支持fiber的积极发展:

  1. GitHub Star添加到项目中。
  2. 在Twitter上发布有关项目的推文
  3. Medium Dev.to或个人博客上写评论或教程。
  4. 帮助我们将此README 文件API文档翻译成另一种语言。

Buy Me A Coffee

星星

Stars over time

⚠️ 许可证

Fiber是根据MIT许可证许可的免费开源软件。