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
Copy file name to clipboardExpand all lines: docs/core/extra/faq.md
+13-1Lines changed: 13 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -177,7 +177,19 @@ For more information, see issue [#750](https://github.com/gofiber/fiber/issues/7
177
177
178
178
## How can I handle conversions between Fiber and net/http?
179
179
180
-
The `adaptor` middleware provides utilities for converting between Fiber and `net/http`. It allows seamless integration of `net/http` handlers, middleware, and requests into Fiber applications, and vice versa.
180
+
Fiber can register common `net/http` handlers directly—just pass an
181
+
`http.Handler`, `http.HandlerFunc`, compatible function, or even a native
182
+
`fasthttp.RequestHandler` to your routing method. For other interoperability scenarios, the `adaptor` middleware provides
183
+
utilities for converting between Fiber and `net/http`. It allows seamless
184
+
integration of `net/http` handlers, middleware, and requests into Fiber
185
+
applications, and vice versa.
186
+
187
+
:::caution Performance trade-offs
188
+
Converted `net/http` handlers run through a compatibility layer. They won't expose
189
+
`fiber.Ctx` or Fiber-specific helpers, and the extra adaptation work makes them slower
190
+
than native Fiber handlers. Use them when interoperability matters, but prefer Fiber
Handlers can be native Fiber handlers (`func(fiber.Ctx) error`), familiar `net/http`
31
+
shapes such as `http.Handler`, `http.HandlerFunc`, or
32
+
`func(http.ResponseWriter, *http.Request)`, and even plain `fasthttp.RequestHandler`
33
+
functions. Fiber automatically adapts supported handlers for you during registration.
34
+
35
+
:::caution Compatibility overhead
36
+
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.
40
+
:::
41
+
30
42
```go title="Examples"
31
43
// Simple GET handler
32
44
app.Get("/api/list", func(c fiber.Ctx) error {
33
45
return c.SendString("I'm a GET request!")
34
46
})
35
47
48
+
// Reuse an existing net/http handler without manual adaptation
49
+
httpHandler:= http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
In `v2` one handler was already mandatory when the route has been registered, but this was checked at runtime and was not correctly reflected in the signature, this has now been changed in `v3` to make it more explicit.
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.
328
305
329
306
### Route chaining
330
307
331
-
This release introduces a dedicated `RouteChain`helper, inspired by [`Express`](https://expressjs.com/en/api.html#app.route), for declaring a stack of handlers on the same path. The original `Route` helper for prefix encapsulation also remains available.
308
+
`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.
332
309
333
310
```go
334
311
RouteChain(path string) Register
@@ -394,7 +371,7 @@ To enable the routing changes above we had to slightly adjust the signature of t
0 commit comments