Skip to content

Commit 158eaff

Browse files
Add docs from gofiber/fiber@a42ddc1
1 parent 1fc2fd0 commit 158eaff

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

docs/core/api/ctx.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,54 @@ app.Get("/", func(c fiber.Ctx) error {
484484
})
485485
```
486486

487+
## End
488+
489+
End immediately flushes the current response and closes the underlying connection.
490+
491+
```go title="Signature"
492+
func (c fiber.Ctx) End() error
493+
```
494+
495+
```go title="Example"
496+
app.Get("/", func(c fiber.Ctx) error {
497+
c.SendString("Hello World!")
498+
return c.End()
499+
})
500+
```
501+
502+
:::caution
503+
Calling `c.End()` will disallow further writes to the underlying connection.
504+
:::
505+
506+
End can be used to stop a middleware from modifying a response of a handler/other middleware down the method chain
507+
when they regain control after calling `c.Next()`.
508+
509+
```go title="Example"
510+
// Error Logging/Responding middleware
511+
app.Use(func(c fiber.Ctx) error {
512+
err := c.Next()
513+
514+
// Log errors & write the error to the response
515+
if err != nil {
516+
log.Printf("Got error in middleware: %v", err)
517+
return c.Writef("(got error %v)", err)
518+
}
519+
520+
// No errors occured
521+
return nil
522+
})
523+
524+
// Handler with simulated error
525+
app.Get("/", func(c fiber.Ctx) error {
526+
// Closes the connection instantly after writing from this handler
527+
// and disallow further modification of its response
528+
defer c.End()
529+
530+
c.SendString("Hello, ... I forgot what comes next!")
531+
return errors.New("some error")
532+
})
533+
```
534+
487535
## Format
488536

489537
Performs content-negotiation on the [Accept](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) HTTP header. It uses [Accepts](ctx.md#accepts) to select a proper format from the supplied offers. A default handler can be provided by setting the `MediaType` to `"default"`. If no offers match and no default is provided, a 406 (Not Acceptable) response is sent. The Content-Type is automatically set when a handler is selected.

docs/core/whats_new.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ testConfig := fiber.TestConfig{
341341
- **String**: Similar to Express.js, converts a value to a string.
342342
- **ViewBind**: Binds data to a view, replacing the old `Bind` method.
343343
- **CBOR**: Introducing [CBOR](https://cbor.io/) binary encoding format for both request & response body. CBOR is a binary data serialization format which is both compact and efficient, making it ideal for use in web applications.
344+
- **End**: Similar to Express.js, immediately flushes the current response and closes the underlying connection.
344345

345346
### Removed Methods
346347

@@ -403,6 +404,41 @@ app.Get("/sse", func(c fiber.Ctx) {
403404

404405
You can find more details about this feature in [/docs/api/ctx.md](./api/ctx.md).
405406

407+
### End
408+
409+
In v3, we introduced a new method to match the Express.js API's `res.end()` method.
410+
411+
```go
412+
func (c Ctx) End()
413+
```
414+
415+
With this method, you can:
416+
417+
- Stop middleware from controlling the connection after a handler further up the method chain
418+
by immediately flushing the current response and closing the connection.
419+
- Use `return c.End()` as an alternative to `return nil`
420+
421+
```go
422+
app.Use(func (c fiber.Ctx) error {
423+
err := c.Next()
424+
if err != nil {
425+
log.Println("Got error: %v", err)
426+
return c.SendString(err.Error()) // Will be unsuccessful since the response ended below
427+
}
428+
return nil
429+
})
430+
431+
app.Get("/hello", func (c fiber.Ctx) error {
432+
query := c.Query("name", "")
433+
if query == "" {
434+
c.SendString("You don't have a name?")
435+
c.End() // Closes the underlying connection
436+
return errors.New("No name provided")
437+
}
438+
return c.SendString("Hello, " + query + "!")
439+
})
440+
```
441+
406442
---
407443

408444
## 🌎 Client package

0 commit comments

Comments
 (0)