πŸ”Œ Routing

Routing refers to how an application's endpoints (URIs) respond to client requests.

Paths

Route paths, combined with a request method, define the endpoints at which requests can be made. Route paths can be strings or string patterns.

Examples of route paths based on strings

// This route path will match requests to the root route, "/":
app.Get("/", func(c *fiber.Ctx) {
c.Send("root")
})
​
// This route path will match requests to "/about":
app.Get("/about", func(c *fiber.Ctx) {
c.Send("about")
})
​
// This route path will match requests to "/random.txt":
app.Get("/random.txt", func(c *fiber.Ctx) {
c.Send("random.txt")
})

Parameters

Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The obtained values can be retrieved using the Params function, with the name of the route parameter specified in the path as their respective keys.

The name of the route parameter must be made up of characters ([A-Za-z0-9_]).

Example of define routes with route parameters

// Parameters
app.Get("/user/:name/books/:title", func(c *fiber.Ctx) {
c.Write(c.Params("name"))
c.Write(c.Params("title"))
})
// Wildcard
app.Get("/user/*", func(c *fiber.Ctx) {
c.Send(c.Params("*"))
})
// Optional parameter
app.Get("/user/:name?", func(c *fiber.Ctx) {
c.Send(c.Params("name"))
})

Since the hyphen (-) and the dot (.) are interpreted literally, they can be used along with route parameters for useful purposes.

// http://localhost:3000/plantae/prunus.persica
app.Get("/plantae/:genus.:species", func(c *fiber.Ctx) {
c.Params("genus") // prunus
c.Params("species") // persica
})
// http://localhost:3000/flights/LAX-SFO
app.Get("/flights/:from-:to", func(c *fiber.Ctx) {
c.Params("from") // LAX
c.Params("to") // SFO
})

Middleware

Functions that are designed to make changes to the request or response are called middleware functions. The Next is a Fiber router function, when called, executes the next function that matches the current route.

Example of a middleware function

app.Use(func(c *fiber.Ctx) {
// Set some security headers:
c.Set("X-XSS-Protection", "1; mode=block")
c.Set("X-Content-Type-Options", "nosniff")
c.Set("X-Download-Options", "noopen")
c.Set("Strict-Transport-Security", "max-age=5184000")
c.Set("X-Frame-Options", "SAMEORIGIN")
c.Set("X-DNS-Prefetch-Control", "off")
​
// Go to next middleware:
c.Next()
​
// End of the chain
fmt.Println("Bye πŸ‘‹!")
})
​
app.Get("/", func(c *fiber.Ctx) {
c.Send("Hello, World!")
})

Use method path is a mount, or prefix path, and limits middleware to only apply to any paths requested that begin with it.

Grouping

If you have many endpoints, you can organize your routes using Group.

func main() {
app := fiber.New()
​
api := app.Group("/api", cors()) // /api
​
v1 := api.Group("/v1", mysql()) // /api/v1
v1.Get("/list", handler) // /api/v1/list
v1.Get("/user", handler) // /api/v1/user
​
v2 := api.Group("/v2", mongodb()) // /api/v2
v2.Get("/list", handler) // /api/v2/list
v2.Get("/user", handler) // /api/v2/user
​
app.Listen(3000)
}