|
| 1 | +--- |
| 2 | +description: Een API-documentatie om te beginnen met het bouwen van web-apps met Fiber. |
| 3 | +--- |
| 4 | + |
| 5 | +# π Aan de slag |
| 6 | + |
| 7 | +[](https://github.com/gofiber/fiber/releases)  [](https://gocover.io/github.com/gofiber/fiber) [](https://travis-ci.org/gofiber/fiber) [](https://travis-ci.org/gofiber/fiber) |
| 8 | + |
| 9 | +**Fiber** is een **web framework** geΓ―nspireerd door [Express](https://github.com/expressjs/express) gebouwd bovenop [Fasthttp](https://github.com/valyala/fasthttp), de **snelste** HTTP-engine voor [Go](https://golang.org/doc/). Ontworpen om **snelle** ontwikkeling **gemakkelijker** te maken **zonder geheugenallocatie** tezamen met **hoge prestaties**. |
| 10 | + |
| 11 | +## Installatie |
| 12 | + |
| 13 | +Allereerst, [download](https://golang.org/dl/) en installeer Go. `1.11` of hoger is vereist. |
| 14 | + |
| 15 | +Installatie wordt gedaan met behulp van het [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) commando: |
| 16 | + |
| 17 | +```bash |
| 18 | +go get -u github.com/gofiber/fiber |
| 19 | +``` |
| 20 | + |
| 21 | +## Hallo, Wereld! |
| 22 | + |
| 23 | +Hieronder staat de eenvoudigste **Fiber**-app ingesloten, welke jij kunt maken. |
| 24 | + |
| 25 | +```go |
| 26 | +package main |
| 27 | + |
| 28 | +import "github.com/gofiber/fiber" |
| 29 | + |
| 30 | +func main() { |
| 31 | + app := fiber.New() |
| 32 | + |
| 33 | + app.Get("/", func(c *fiber.Ctx) { |
| 34 | + c.Send("Hallo, Wereld!") |
| 35 | + }) |
| 36 | + |
| 37 | + app.Listen(3000) |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +```text |
| 42 | +go run server.go |
| 43 | +``` |
| 44 | + |
| 45 | +Navigeer naar `http://localhost:3000` en je zal `Hallo, Wereld!` op de pagina zien verschijnen. |
| 46 | + |
| 47 | +## Basis routering |
| 48 | + |
| 49 | +Routing verwijst naar het bepalen hoe een applicatie reageert op een client request naar een bepaald endpoint, dat is een URI (of pad) en een specifieke HTTP request methode (GET, PUT, POST enzovoort). |
| 50 | + |
| 51 | +{% hint style="info" %} |
| 52 | +Elke route kan **meerdere handler functies** hebben die worden uitgevoerd wanneer de route overeenkomt. |
| 53 | +{% endhint %} |
| 54 | + |
| 55 | +Routedefinities hebben de volgende structuren: |
| 56 | + |
| 57 | +```go |
| 58 | +// Functie signatuur |
| 59 | +app.Method(path string, ...func(*fiber.Ctx)) |
| 60 | +``` |
| 61 | + |
| 62 | +* `app` is een instance van **Fiber**. |
| 63 | +* `Method` is een [HTTP request methode](https://fiber.wiki/application#methods), met hoofdletters: `Get`, `Put`, `Post`, etc. |
| 64 | +* `path` is een virtueel pad op de server. |
| 65 | +* `func(*fiber.Ctx)` is een callback-functie die de [Context](https://fiber.wiki/context) bevat die wordt uitgevoerd wanneer de route overeenkomt. |
| 66 | + |
| 67 | +**Simpele route** |
| 68 | + |
| 69 | +```go |
| 70 | +// Antwoord met "Hallo, Wereld!" op rootpad, "/" |
| 71 | +app.Get("/", func(c *fiber.Ctx) { |
| 72 | + c.Send("Hallo, Wereld!") |
| 73 | +}) |
| 74 | +``` |
| 75 | + |
| 76 | +**Parameters** |
| 77 | + |
| 78 | +```go |
| 79 | +// GET http://localhost:8080/hallo%20wereld |
| 80 | + |
| 81 | +app.Get("/:value", func(c *fiber.Ctx) { |
| 82 | + c.Send("Request ontvangen met waarde: " + c.Params("value")) |
| 83 | + // => Request ontvangen met waarde: hallo wereld |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +**Optionele parameter** |
| 88 | + |
| 89 | +```go |
| 90 | +// GET http://localhost:3000/john |
| 91 | + |
| 92 | +app.Get("/:naam?", func(c *fiber.Ctx) { |
| 93 | + if c.Params("naam") != "" { |
| 94 | + c.Send("Hallo " + c.Params("naam")) |
| 95 | + // => Hallo john |
| 96 | + } else { |
| 97 | + c.Send("Waar is john?") |
| 98 | + } |
| 99 | +}) |
| 100 | +``` |
| 101 | + |
| 102 | +**Wildcards** |
| 103 | + |
| 104 | +```go |
| 105 | +// GET http://localhost:3000/api/gebruiker/john |
| 106 | + |
| 107 | +app.Get("/api/*", func(c *fiber.Ctx) { |
| 108 | + c.Send("API pad: " + c.Params("*")) |
| 109 | + // => API pad: gebruiker/john |
| 110 | +}) |
| 111 | +``` |
| 112 | + |
| 113 | +## Statische bestanden |
| 114 | + |
| 115 | +Vervang de functie handler door een bestand of directory string om statische bestanden zoals **afbeeldingen**, **CSS** en **JavaScript** bestanden weer te geven. |
| 116 | + |
| 117 | +Functie signatuur: |
| 118 | + |
| 119 | +```go |
| 120 | +app.Static(prefix, root string) |
| 121 | +``` |
| 122 | + |
| 123 | +Gebruik de volgende code om bestanden weer te geven in een map met de naam `./public`: |
| 124 | + |
| 125 | +```go |
| 126 | +app := fiber.New() |
| 127 | + |
| 128 | +app.Static("/", "./public") |
| 129 | + |
| 130 | +app.Listen(8080) |
| 131 | +``` |
| 132 | + |
| 133 | +Nu kunt u door de bestanden in de map `./public` bladeren: |
| 134 | + |
| 135 | +```bash |
| 136 | +http://localhost:8080/hello.html |
| 137 | +http://localhost:8080/js/jquery.js |
| 138 | +http://localhost:8080/css/style.css |
| 139 | +``` |
| 140 | + |
0 commit comments