Learning Fiber - Advanced Routing and Handler Parameters
Episode 7 of 23

Learning Fiber - Advanced Routing and Handler Parameters

This episode dissects advanced Fiber v3 routing: all HTTP methods, app.Add and app.All, registering many handlers on one route, variadic handlers, multiple routes in one call, and the limit on the number of handlers that can be registered per route.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

In episode 4 you already met path parameters and constraints. Episode 7 completes the routing foundation: how to register routes for every HTTP method, combine multiple handlers on one route, use variadic handlers, and understand the limit on how many handlers can be registered.

This topic is often underrated, yet the variadic handler and multiple-route-in-one-call patterns appear constantly when reading Fiber open-source code. Mastering them makes you fluent at reading and writing complex APIs.

HTTP Methods in Fiber

The List of Available Methods

Fiber v3 provides a method for every standard HTTP verb. Here's the mapping:

Semua metode HTTP
app.Get("/resource", listHandler)       // READ
app.Post("/resource", createHandler)    // CREATE
app.Put("/resource/:id", updateHandler) // UPDATE menyeluruh
app.Patch("/resource/:id", patchHandler)
app.Delete("/resource/:id", deleteHandler)
app.Options("/resource", optionsHandler)
app.Head("/resource", headHandler)
app.Trace("/resource", traceHandler)
app.Connect("/resource", connectHandler)

Besides the explicit methods, there are two important helpers. app.All(path, ...handler) registers a route that matches any method, and app.Add(method, path, ...handler) accepts the method as a string — useful when the method is determined dynamically from data.

Routes with Multiple Handlers

One route can have several handlers. Fiber executes them sequentially in a single pipeline — the first handler can act as route-specific middleware:

Multiple handler per route
func checkBody(c fiber.Ctx) error {
    if len(c.Body()) == 0 {
        return c.Status(fiber.StatusBadRequest).SendString("body kosong")
    }
    return c.Next()
}
 
app.Post("/submit", checkBody, func(c fiber.Ctx) error {
    return c.JSON(fiber.Map{"ok": true, "data": c.Body()})
})

checkBody validates the request before the main handler runs. Because it's passed directly to the route, this middleware only applies to /submit, not to all routes.

Variadic Handlers

Registering Many Routes at Once

Fiber v3 accepts a diverse list of handlers in one call, including combinations of route strings and fiber.Handler:

Handler variadic
app.Get("/a", handlerA, "/b", handlerB, handlerC)

This pattern makes app.Get("/a", handlerA, "/b", handlerB, handlerC) register two routes at once: /a with handlerA, then /b with handlerB and handlerC in sequence. This capability comes from the variadic handler signature definition:

Signature handler variadic
func (app *App) Get(path string, handlers ...any) Routes

The variadic ...any accepts both strings (a new path) and fiber.Handler. This is useful when you want to register routes with concise lines, for example a set of simple routes handling one resource.

Converting Ordinary Handlers

Not every handler function has the fiber.Handler shape. Fiber provides conversion functions so handlers from other libraries can be used directly:

Konversi handler
app.Get("/nethttp", adaptor.HTTPHandler(handler))
app.Get("/gin", adaptor.HTTPHandlerFunc(writeGinLikeResponse))

The github.com/gofiber/fiber/v3/adaptor package provides HTTPHandler, HTTPHandlerFunc, HTTPMiddleware, and HTTPMiddlewareFunc to adapt net/http and Gin handlers to Fiber. This helps a lot during gradual migration from other frameworks.

The Handler Limit per Route

The FiberConfig Routes Setting

Fiber limits the number of handlers that can be registered on a single route. The default is 5 and it can be changed via configuration:

Mengatur batas handler
app := fiber.New(fiber.Config{
    Routes: fiber.Routes{
        UsePathParams: true,
        StrictRouting: false,
        CaseSensitive: false,
        RouteHandlerLimit: 10,
    },
})

RouteHandlerLimit: 10 raises the handler limit per route from the default of 5 to 10. If you register more handlers than the limit, Fiber returns an error at startup. The rest — UsePathParams enables parameters on app.Use middleware, and StrictRouting/CaseSensitive control the strictness of path matching.

Testing

Tes variadic dan multiple handler
curl http://localhost:3000/a
curl -X POST http://localhost:3000/submit
curl -X POST -d '{"x":1}' http://localhost:3000/submit
curl http://localhost:3000/nethttp

The first request to /a runs handlerA, registered variadically. A POST to /submit without a body returns 400 from checkBody; with a body it returns {"ok":true}. The /nethttp endpoint proves that a net/http handler can be adapted directly.

Closing

Key takeaways:

  • Fiber provides methods for every HTTP verb: Get, Post, Put, Patch, Delete, Options, Head, Trace, Connect.
  • app.All matches all methods; app.Add(method, path, ...) accepts a dynamic method.
  • One route can have many handlers executed sequentially in a single pipeline.
  • Variadic handlers enable app.Get("/a", hA, "/b", hB) to register many routes at once.
  • The adaptor package converts net/http and Gin handlers to fiber.Handler.
  • Routes.RouteHandlerLimit controls the handler count limit per route, default 5.

In the next episode, episode 8, we discuss groups and domain routing — organizing routes into RouteGroup, nested groups, mounting sub-apps, and the v3 features RouteChain and per-host domain routing.