Skip to content

🤗 Is there a way to get the final route path in the middleware of the group #923

@michaelzx

Description

@michaelzx

Question description
go v1.15.1
fiber v2.0.6

Code snippet Optional

package main

import (
	"fmt"
	"github.com/gofiber/fiber/v2"
)

func handler(ctx *fiber.Ctx) error {
	fmt.Println(ctx.Route().Path)
	// output2: /api/v1/user/:ID
	return ctx.SendString("ok")
}
func main() {
	app := fiber.New()

	api := app.Group("/api") // /api

	v1 := api.Group("/v1") // /api/v1
	v1.Use(func(ctx *fiber.Ctx) error {
		fmt.Println(ctx.Route().Path)
		// output1: /api/v1
		return ctx.Next()
	})
	v1.Get("/list", handler) // /api/v1/list
	v1.Get("/user/:ID", handler) // /api/v1/user

	v2 := api.Group("/v2")   // /api/v2
	v2.Get("/list", handler) // /api/v2/list
	v2.Get("/user", handler) // /api/v2/user

	app.Listen(":3000")
}

If I request http://127.0.0.1:3000/api/v1/user/1,
Can I get /api/v1/user/:ID at output1?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions