You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adapted `net/http` handlers execute through a compatibility layer. They don't receive
37
-
`fiber.Ctx` or gain access to Fiber-specific APIs, and the conversion adds more
38
-
overhead than running a native `fiber.Handler`. Because they cannot call `c.Next()`, they will also terminate the handler chain. Prefer Fiber handlers when you need the
39
-
lowest latency or Fiber features.
63
+
Fiber adapts these handlers through `fasthttpadaptor`. They do not receive
64
+
`fiber.Ctx`, cannot call `c.Next()`, and therefore always terminate the handler
65
+
chain. The compatibility layer also adds more overhead than running a native
66
+
Fiber handler, so prefer the other forms when possible.
40
67
:::
41
68
69
+
### fasthttp handlers (cases 12–13)
70
+
71
+
- **Case 12.** `fasthttp.RequestHandler`
72
+
- **Case 13.** `func(*fasthttp.RequestCtx) error`
73
+
74
+
fasthttp handlers run with full access to the underlying `fasthttp.RequestCtx`.
75
+
They are expected to manage the response directly. Fiber will propagate any
76
+
error returned by the `func(*fasthttp.RequestCtx) error` variant but otherwise
77
+
does not inspect the context state.
78
+
42
79
```go title="Examples"
43
-
// Simple GET handler
80
+
// Simple GET handler (Fiber accepts both func(fiber.Ctx) and func(fiber.Ctx) error)
Copy file name to clipboardExpand all lines: docs/core/whats_new.md
+18Lines changed: 18 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -303,6 +303,24 @@ We have slightly adapted our router interface
303
303
304
304
Fiber now ships with a routing adapter (see `adapter.go`) that understands native Fiber handlers alongside `net/http` and `fasthttp` handlers. Route registration helpers accept a required `handler` argument plus optional additional `handlers`, all typed as `any`, and the adapter transparently converts supported handler styles so you can keep using the ecosystem functions you're familiar with.
305
305
306
+
To align even closer with Express, you can also register handlers that accept the new `fiber.Req` and `fiber.Res` helper interfaces. The adapter understands both two-argument (`func(fiber.Req, fiber.Res)`) and three-argument (`func(fiber.Req, fiber.Res, func() error)`) callbacks, regardless of whether they return an `error`. When you include the optional `next` callback, Fiber wires it to `c.Next()` for you so middleware continues to behave as expected. If your handler returns an `error`, the value returned from the injected `next()` bubbles straight back to the caller. When your handler omits an `error` return, Fiber records the result of `next()` and returns it after your function exits so downstream failures still propagate.
307
+
308
+
| Case | Handler signature | Notes |
309
+
| ---- | ----------------- | ----- |
310
+
| 1 |`fiber.Handler`| Native Fiber handler. |
311
+
| 2 |`func(fiber.Ctx)`| Fiber handler without an error return. |
| 5 |`func(fiber.Req, fiber.Res, func() error) error`| Express-style middleware with an error-returning `next` callback and handler error return. |
315
+
| 6 |`func(fiber.Req, fiber.Res, func() error)`| Express-style middleware with an error-returning `next` callback. |
316
+
| 7 |`func(fiber.Req, fiber.Res, func()) error`| Express-style middleware with a no-argument `next` callback and handler error return. |
317
+
| 8 |`func(fiber.Req, fiber.Res, func())`| Express-style middleware with a no-argument `next` callback. |
318
+
| 9 |`http.HandlerFunc`| Standard-library handler function adapted through `fasthttpadaptor`. |
319
+
| 10 |`http.Handler`| Standard-library handler implementation; pointer receivers must be non-nil. |
320
+
| 11 |`func(http.ResponseWriter, *http.Request)`| Standard-library function handlers via `fasthttpadaptor`. |
321
+
| 12 |`fasthttp.RequestHandler`| Direct fasthttp handler without error return. |
322
+
| 13 |`func(*fasthttp.RequestCtx) error`| fasthttp handler that returns an error to Fiber. |
323
+
306
324
### Route chaining
307
325
308
326
`RouteChain` is a new helper inspired by [`Express`](https://expressjs.com/en/api.html#app.route) that makes it easy to declare a stack of handlers on the same path, while the existing `Route` helper stays available for prefix encapsulation.
0 commit comments