| 1 | --- |
| 2 | id: route-handlers |
| 3 | title: Route Handlers |
| 4 | --- |
| 5 | |
| 6 | Registers a route bound to a specific [HTTP method](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). |
| 7 | |
| 8 | ```go title="Signatures" |
| 9 | // HTTP methods |
| 10 | func (app *App) Get(path string, handlers ...Handler) Router |
| 11 | func (app *App) Head(path string, handlers ...Handler) Router |
| 12 | func (app *App) Post(path string, handlers ...Handler) Router |
| 13 | func (app *App) Put(path string, handlers ...Handler) Router |
| 14 | func (app *App) Delete(path string, handlers ...Handler) Router |
| 15 | func (app *App) Connect(path string, handlers ...Handler) Router |
| 16 | func (app *App) Options(path string, handlers ...Handler) Router |
| 17 | func (app *App) Trace(path string, handlers ...Handler) Router |
| 18 | func (app *App) Patch(path string, handlers ...Handler) Router |
| 19 | |
| 20 | // Add allows you to specifiy a method as value |
| 21 | func (app *App) Add(method, path string, handlers ...Handler) Router |
| 22 | |
| 23 | // All will register the route on all HTTP methods |
| 24 | // Almost the same as app.Use but not bound to prefixes |
| 25 | func (app *App) All(path string, handlers ...Handler) Router |
| 26 | ``` |
| 27 | |
| 28 | ```go title="Examples" |
| 29 | // Simple GET handler |
| 30 | app.Get("/api/list", func(c *fiber.Ctx) error { |
| 31 | return c.SendString("I'm a GET request!") |
| 32 | }) |
| 33 | |
| 34 | // Simple POST handler |
| 35 | app.Post("/api/register", func(c *fiber.Ctx) error { |
| 36 | return c.SendString("I'm a POST request!") |
| 37 | }) |
| 38 | ``` |
| 39 | |
| 40 | **Use** can be used for middleware packages and prefix catchers. These routes will only match the beginning of each path i.e. `/john` will match `/john/doe`, `/johnnnnn` etc |
| 41 | |
| 42 | ```go title="Signature" |
| 43 | func (app *App) Use(args ...interface{}) Router |
| 44 | ``` |
| 45 | |
| 46 | ```go title="Examples" |
| 47 | // Match any request |
| 48 | app.Use(func(c *fiber.Ctx) error { |
| 49 | return c.Next() |
| 50 | }) |
| 51 | |
| 52 | // Match request starting with /api |
| 53 | app.Use("/api", func(c *fiber.Ctx) error { |
| 54 | return c.Next() |
| 55 | }) |
| 56 | |
| 57 | // Match requests starting with /api or /home (multiple-prefix support) |
| 58 | app.Use([]string{"/api", "/home"}, func(c *fiber.Ctx) error { |
| 59 | return c.Next() |
| 60 | }) |
| 61 | |
| 62 | // Attach multiple handlers |
| 63 | app.Use("/api", func(c *fiber.Ctx) error { |
| 64 | c.Set("X-Custom-Header", random.String(32)) |
| 65 | return c.Next() |
| 66 | }, func(c *fiber.Ctx) error { |
| 67 | return c.Next() |
| 68 | }) |
| 69 | ``` |