Skip to content

Commit b4b8b4b

Browse files
Fennygitbook-bot
authored andcommitted
GitBook: [master] 4 pages modified
1 parent 01d7d34 commit b4b8b4b

File tree

4 files changed

+89
-81
lines changed

4 files changed

+89
-81
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333
app.Get("/", func(c *fiber.Ctx) {
3434
c.Send("Hello, World!")
3535
})
36-
36+
3737
app.Listen(3000)
3838
}
3939
```
@@ -65,7 +65,7 @@ app.Method(path string, ...func(*fiber.Ctx))
6565
* `path` is a path on the server.
6666
* `func(*fiber.Ctx)` is a callback function containing the [Context](https://fiber.wiki/context) executed when the route is matched.
6767

68-
### Simple route
68+
**Simple route**
6969

7070
```go
7171
// Respond with "Hello, World!" on root path, "/":
@@ -74,7 +74,7 @@ app.Get("/", func(c *fiber.Ctx) {
7474
})
7575
```
7676

77-
### Route with parameter
77+
**Route with parameter**
7878

7979
```go
8080
// GET http://localhost:8080/hello%20world
@@ -85,7 +85,7 @@ app.Get("/:value", func(c *fiber.Ctx) {
8585
})
8686
```
8787

88-
### Route with optional parameter
88+
**Route with optional parameter**
8989

9090
```go
9191
// GET http://localhost:8080/hello%20world
@@ -101,7 +101,7 @@ app.Get("/:value?", func(c *fiber.Ctx) {
101101
})
102102
```
103103

104-
### Route with wildcard
104+
**Route with wildcard**
105105

106106
```go
107107
// GET http://localhost:8080/api/user/john

application.md

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ func main() {
4343
Prefork: true,
4444
CaseSensitive: true,
4545
StrictRouting: true,
46-
ServerHeader: "Go",
47-
// etc...
46+
ServerHeader: "Fiber",
47+
// ...
4848
})
4949

5050
// Or change Settings after initiating app
5151
app.Settings.Prefork = true
5252
app.Settings.CaseSensitive = true
5353
app.Settings.StrictRouting = true
54-
app.Settings.ServerHeader = true
55-
// etc...
54+
app.Settings.ServerHeader = "Fiber"
55+
// ...
5656

5757
app.Listen(3000)
5858
}
@@ -168,7 +168,7 @@ func main() {
168168
<td style="text-align:left"><code>string</code>
169169
</td>
170170
<td style="text-align:left">If you preset the template file extension, you do not need to provide
171-
the full filename in the Render function: <code>c.Render(&quot;home&quot;, d) -&gt; home.pug</code>
171+
the full filename in the Render function: <code>c.Render(&quot;home&quot;, data) -&gt; home.pug</code>
172172
</td>
173173
<td style="text-align:left"><code>&quot;html&quot;</code>
174174
</td>
@@ -185,7 +185,6 @@ By default, this method will send `index.html` files in response to a request on
185185
**Signature**
186186

187187
```go
188-
app.Static(root string) // => without prefix
189188
app.Static(prefix, root string) // => with prefix
190189
```
191190

@@ -194,7 +193,7 @@ app.Static(prefix, root string) // => with prefix
194193
Use the following code to serve files in a directory named `./public`
195194

196195
```go
197-
app.Static("./public")
196+
app.Static("/", "./public")
198197

199198
// => http://localhost:3000/hello.html
200199
// => http://localhost:3000/js/jquery.js
@@ -205,17 +204,17 @@ To serve from multiple directories, you can use **Static** multiple times.
205204

206205
```go
207206
// Serve files from "./public" directory:
208-
app.Static("./public")
207+
app.Static("/", "./public")
209208

210209
// Serve files from "./files" directory:
211-
app.Static("./files")
210+
app.Static("/", "./files")
212211
```
213212

214213
{% hint style="info" %}
215214
Use a reverse proxy cache like [NGINX](https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/) to improve performance of serving static assets.
216215
{% endhint %}
217216

218-
To create a virtual path prefix \(_where the path does not actually exist in the file system_\) for files that are served by the **Static** method, specify a prefix path for the static directory, as shown below:
217+
You can use any virtual path prefix \(_where the path does not actually exist in the file system_\) for files that are served by the **Static** method, specify a prefix path for the static directory, as shown below:
219218

220219
```go
221220
app.Static("/static", "./public")
@@ -232,8 +231,8 @@ Routes an HTTP request, where **METHOD** is the [HTTP method](https://developer.
232231
**Signature**
233232

234233
```go
235-
/* These methods support :param & :optional? in path
236-
You are required to pass a path to each method */
234+
// HTTP methods support :param, :optional? and *wildcards
235+
// You are required to pass a path to each method
237236
app.All(path string, handlers ...func(*Ctx))
238237
app.Get(...
239238
app.Put(...
@@ -245,9 +244,9 @@ app.Delete(...
245244
app.Connect(...
246245
app.Options(...
247246

248-
/* Use will only match the prefix of each path
249-
i.e. "/john" will match "/john/doe", "/johnnnn"
250-
Use does not support :param & :optional? in path */
247+
// Use() will only match the beggining of each path
248+
// i.e. "/john" will match "/john/doe", "/johnnnn"
249+
// Use() does not support :param & :optional? in path
251250
app.Use(handlers ...func(*Ctx))
252251
app.Use(prefix string, handlers ...func(*Ctx))
253252
```
@@ -295,7 +294,7 @@ func main() {
295294
app := fiber.New()
296295
// Optional middleware
297296
app.Use("/ws", func(c *fiber.Ctx) {
298-
if c.Get("host") == "localhost:3000" {
297+
if c.Get("host") != "localhost:3000" {
299298
c.Status(403).Send("Request origin not allowed")
300299
} else {
301300
c.Next()
@@ -352,47 +351,14 @@ func main() {
352351
}
353352
```
354353

355-
## Recover
356-
357-
You can recover from panic errors in any handler by registering a `Recover` method. You can access the panic error by calling [`Error()`](https://github.com/gofiber/docs/tree/2f0839895190c02779e91237531b27445d4427c6/context/README.md#error)
358-
359-
{% hint style="info" %}
360-
Recover is disabled by default unless you register a handler.
361-
{% endhint %}
362-
363-
**Signature**
364-
365-
```go
366-
app.Recover(handler func(*Ctx))
367-
```
368-
369-
**Example**
370-
371-
```go
372-
func main() {
373-
app := fiber.New()
374-
375-
app.Get("/", func(c *fiber.Ctx) {
376-
panic("Something went wrong!")
377-
})
378-
379-
app.Recover(func(c *fiber.Ctx) {
380-
c.Status(500).Send(c.Error())
381-
// => 500 "Something went wrong!"
382-
})
383-
384-
app.Listen(3000)
385-
}
386-
```
387-
388354
## Listen
389355

390356
Binds and listens for connections on the specified address. This can be a `int` for port or `string` for address.
391357

392358
**Signature**
393359

394360
```go
395-
app.Listen(address interface{}, tls ...string)
361+
app.Listen(address interface{}, tls ...*tls.Config)
396362
```
397363

398364
**Example**
@@ -404,10 +370,16 @@ app.Listen(":8080")
404370
app.Listen("127.0.0.1:8080")
405371
```
406372

407-
To enable **TLS/HTTPS** you can append your **cert** and **key** path.
373+
To enable **TLS/HTTPS** you can append a ****[**TLS config**](https://golang.org/pkg/crypto/tls/#Config).
408374

409375
```go
410-
app.Listen(443, "server.crt", "server.key")
376+
cer, err := tls.LoadX509KeyPair("server.crt", "server.key")
377+
if err != nil {
378+
log.Fatal(err)
379+
}
380+
config := &tls.Config{Certificates: []tls.Certificate{cer}}
381+
382+
app.Listen(443, config)
411383
```
412384

413385
## Test

context.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ Fiber provides similar functions for the other accept headers.
4949
app.Get("/", func(c *fiber.Ctx) {
5050
c.AcceptsCharsets("utf-16", "iso-8859-1")
5151
// "iso-8859-1"
52-
52+
5353
c.AcceptsEncodings("compress", "br")
5454
// "compress"
55-
55+
5656
c.AcceptsLanguages("pt", "nl", "ru")
5757
// "nl" "ru"
5858
})
@@ -276,8 +276,8 @@ func main() {
276276
})
277277
}
278278
```
279-
```
280279

280+
```text
281281
## Cookie
282282
283283
Set cookies
@@ -290,13 +290,13 @@ c.Cookie(*Cookie)
290290

291291
```go
292292
type Cookie struct {
293-
Name string
294-
Value string
295-
Path string
296-
Domain string
297-
Expires time.Time
298-
Secure bool
299-
HTTPOnly bool
293+
Name string
294+
Value string
295+
Path string
296+
Domain string
297+
Expires time.Time
298+
Secure bool
299+
HTTPOnly bool
300300
}
301301
```
302302

@@ -306,11 +306,11 @@ type Cookie struct {
306306
app.Get("/", func(c *fiber.Ctx) {
307307
// Create cookie
308308
cookie := new(fiber.Cookie)
309-
cookie.Name = "john"
310-
cookie.Value = "doe"
311-
cookie.Expires = time.Now().Add(24 * time.Hour)
312-
// Set cookie
313-
c.Cookie(cookie)
309+
cookie.Name = "john"
310+
cookie.Value = "doe"
311+
cookie.Expires = time.Now().Add(24 * time.Hour)
312+
// Set cookie
313+
c.Cookie(cookie)
314314
})
315315
```
316316

@@ -509,7 +509,7 @@ Not implemented yet, pull requests are welcome!
509509

510510
## Get
511511

512-
Returns the HTTP request header specified by field.
512+
Returns the HTTP request header specified by field.
513513

514514
{% hint style="success" %}
515515
The match is **case-insensitive**.
@@ -1000,11 +1000,11 @@ c.Range(int size)
10001000

10011001
```go
10021002
type Range struct {
1003-
Type string
1004-
Ranges []struct {
1005-
Start int64
1006-
End int64
1007-
}
1003+
Type string
1004+
Ranges []struct {
1005+
Start int64
1006+
End int64
1007+
}
10081008
}
10091009
```
10101010

@@ -1015,7 +1015,7 @@ type Range struct {
10151015
app.Get("/", func(c *fiber.Ctx) {
10161016
b := c.Range(1000)
10171017
if b.Type == "bytes" {
1018-
for r := range r.Ranges {
1018+
for r := range r.Ranges {
10191019
fmt.Println(r)
10201020
// [500, 700]
10211021
}
@@ -1066,7 +1066,7 @@ Renders a template with data and sends a `text/html` response. We support the fo
10661066
c.Render(file string, data interface{}, engine ...string) error
10671067
```
10681068

1069-
A simple template file with the ****`mustache` ****syntax.
1069+
A simple template file with the **`mustache`** syntax.
10701070

10711071
{% code title="/views/home.tmpl" %}
10721072
```go
@@ -1406,7 +1406,7 @@ c.Vary(field ...string)
14061406
app.Get("/", func(c *fiber.Ctx) {
14071407
c.Vary("Origin") // => Vary: Origin
14081408
c.Vary("User-Agent") // => Vary: Origin, User-Agent
1409-
1409+
14101410
// No duplicates
14111411
c.Vary("Origin") // => Vary: Origin, User-Agent
14121412

middleware.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,42 @@ func main() {
204204
}
205205
```
206206

207+
## Recover
208+
209+
You can recover from panic errors within any route. By default the Recover middleware will respond with `500 Internal Server Error` when a panic occurs. You can also provide your own error handler.
210+
211+
**Signature**
212+
213+
```go
214+
middleware.Recover(handle ...func(*Ctx, error)) func(*Ctx)
215+
```
216+
217+
**Example**
218+
219+
```go
220+
package main
221+
222+
import (
223+
"github.com/gofiber/fiber"
224+
"github.com/gofiber/fiber/middleware"
225+
)
226+
227+
func main() {
228+
app := fiber.New()
229+
230+
app.Use(middleware.Recover(func(c *fiber.Ctx, err error) {
231+
log.Println(err) // "Something went wrong!"
232+
c.SendStatus(500) // Internal Server Error
233+
})))
234+
235+
app.Get("/", func(c *fiber.Ctx) {
236+
panic("Something went wrong!")
237+
})
238+
239+
app.Listen(3000)
240+
}
241+
```
242+
207243
## RequestID
208244

209245
RequestID adds an indentifier to the request using the `X-Request-ID` header

0 commit comments

Comments
 (0)