1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-24 09:03:48 +00:00
fiber/.github/README_ja.md
2020-02-22 11:09:32 +08:00

12 KiB
Raw Blame History

Fiber


FIberは、Expressに触発されたWebフレームワークです。Go 最速のHTTPエンジンであるFasthttpで作られています。ゼロメモリアロケーションパフォーマンスを念頭に置いて設計されており、迅速な開発をサポートします。

クイックスタート

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フレームワークやマイクロサービスの構築を始める前に多くを学ばなければなりません。 しかし、 WebフレームワークであるFiberはミニマリズムUNIX哲学をもとに作られているため、新しいGopherはスムーズにGoの世界に入ることができます。

Fiberは人気の高いWebフレームワークであるExpressjsにインスパイアされています。 わたしたちは Expressの手軽さとGoのパフォーマンスを組み合わせました。 もしも、WebフレームワークをExpress等のNode.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に開発支援してくださるなら:

  1. GitHub Starをつけてください 。
  2. あなたのTwitterでプロジェクトについてツイートしてください。
  3. MediumDev.to、または個人のブログでレビューまたはチュートリアルを書いてください。
  4. このREADMEAPIドキュメントを別の言語に翻訳するためにご協力ください。

Buy Me A Coffee

Supporters

Buy Me A Coffee

HenrikBinggl

koddr

MarvinJWendt

ToishY

スター

Stars over time

⚠️ ライセンス

Fiberは、 MIT Licenseに基づいてライセンスされた無料のオープンソースソフトウェアです。