@@ -153,12 +153,12 @@ func main() {
153
153
```
154
154
155
155
### Serve static files
156
-
156
+ https://fiber.wiki/application#static
157
157
``` go
158
158
func main () {
159
159
app := fiber.New ()
160
160
161
- app.Static (" /public" )
161
+ app.Static (" /" , " / public" )
162
162
// => http://localhost:3000/js/script.js
163
163
// => http://localhost:3000/css/style.css
164
164
@@ -174,7 +174,8 @@ func main() {
174
174
```
175
175
176
176
### Middleware & Next
177
-
177
+ https://fiber.wiki/routing#middleware
178
+ https://fiber.wiki/context#next
178
179
``` go
179
180
func main () {
180
181
app := fiber.New ()
@@ -205,9 +206,10 @@ func main() {
205
206
<summary >📚 Show more code examples</summary >
206
207
207
208
### Template engines
209
+ https://fiber.wiki/application#settings
210
+ https://fiber.wiki/context#render
208
211
209
- Already supports:
210
-
212
+ Supported engines:
211
213
- [ html] ( https://golang.org/pkg/html/template/ )
212
214
- [ amber] ( https://github.com/eknkc/amber )
213
215
- [ handlebars] ( https://github.com/aymerick/raymond )
@@ -241,7 +243,7 @@ func main() {
241
243
```
242
244
243
245
### Grouping routes into chains
244
-
246
+ https://fiber.wiki/application#group
245
247
``` go
246
248
func main () {
247
249
app := fiber.New ()
@@ -263,8 +265,8 @@ func main() {
263
265
}
264
266
```
265
267
266
- ### Built-in logger
267
-
268
+ ### Middleware logger
269
+ https://fiber.wiki/middleware#logger
268
270
``` go
269
271
import (
270
272
" github.com/gofiber/fiber"
@@ -288,6 +290,7 @@ func main() {
288
290
```
289
291
290
292
### Cross-Origin Resource Sharing (CORS)
293
+ https://fiber.wiki/middleware#cors
291
294
292
295
[ CORS] ( https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS ) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own.
293
296
@@ -343,35 +346,40 @@ func main() {
343
346
```
344
347
345
348
### JSON Response
346
-
349
+ https://fiber.wiki/context#json
347
350
``` go
351
+ type User struct {
352
+ Name string ` json:"name"`
353
+ Age int ` json:"age"`
354
+ }
355
+
348
356
func main () {
349
357
app := fiber.New ()
350
358
351
- type User struct {
352
- Name string ` json:"name" `
353
- Age int ` json:" age"`
354
- }
359
+ app. Get ( " /user " , func (c *fiber. Ctx ) {
360
+ c. JSON (&User{ " John " , 20 })
361
+ // {"name":"John", " age":20}
362
+ })
355
363
356
- // Serialize JSON
357
364
app.Get (" /json" , func (c *fiber.Ctx ) {
358
- c.JSON (&User{" John" , 20 })
359
- // => {"name":"John", "age":20}
365
+ c.JSON (&fiber.Map {
366
+ " success" : true ,
367
+ " message" : " Hi John!" ,
368
+ })
369
+ // {"success":true, "message":"Hi John!"}
360
370
})
361
371
362
372
app.Listen (3000 )
363
373
}
364
374
```
365
375
366
376
### WebSocket support
367
-
377
+ https://fiber.wiki/application#websocket
368
378
``` go
369
379
func main () {
370
380
app := fiber.New ()
371
381
372
- app.WebSocket (" /ws/:name" , func (c *fiber.Conn ) {
373
- log.Println (c.Params (" name" ))
374
-
382
+ app.WebSocket (" /ws" , func (c *fiber.Conn ) {
375
383
for {
376
384
mt , msg , err := c.ReadMessage ()
377
385
if err != nil {
@@ -389,26 +397,33 @@ func main() {
389
397
}
390
398
})
391
399
392
- // Listen on ws://localhost:3000/ws/john
400
+ // Listen on ws://localhost:3000/ws
393
401
app.Listen (3000 )
394
402
}
395
403
```
396
404
397
- ### Recover from ` panic `
398
-
405
+ ### Recover middleware
406
+ https://fiber.wiki/middleware#recover
399
407
``` go
408
+ package main
409
+
410
+ import (
411
+ " github.com/gofiber/fiber"
412
+ " github.com/gofiber/fiber/middleware"
413
+ )
414
+
400
415
func main () {
401
416
app := fiber.New ()
402
417
418
+ app.Use (middleware.Recover (func (c *fiber.Ctx , err error ) {
419
+ log.Println (err) // "Something went wrong!"
420
+ c.SendStatus (500 ) // Internal Server Error
421
+ })))
422
+
403
423
app.Get (" /" , func (c *fiber.Ctx ) {
404
424
panic (" Something went wrong!" )
405
425
})
406
426
407
- app.Recover (func (c *fiber.Ctx ) {
408
- c.Status (500 ).Send (c.Error ())
409
- // => 500 "Something went wrong!"
410
- })
411
-
412
427
app.Listen (3000 )
413
428
}
414
429
```
@@ -437,6 +452,12 @@ If you want to say **thank you** and/or support the active development of `Fiber
437
452
438
453
<table >
439
454
<tr >
455
+ <td align="center">
456
+ <a href="https://github.com/gofiber/fiber">
457
+ <img src="https://i.stack.imgur.com/frlIf.png" width="100px"></br>
458
+ <sub><b>JustDave</b></sub>
459
+ </a>
460
+ </td>
440
461
<td align="center">
441
462
<a href="https://github.com/bihe">
442
463
<img src="https://avatars1.githubusercontent.com/u/635852?s=460&v=4" width="100px"></br>
0 commit comments