Rewrite


Rewrite 미들웨어는 제공된 규칙에 따라 URL 경로를 다시 작성합니다. 이는 이전 버전과의 호환성을 위해 또는 더 깨끗하고 설명적인 링크를 만드는 데 도움이 될 수 있습니다.

Signatures

func New(config ...Config) fiber.Handler

Config

Property
Type
Description
Default

Next

func(*fiber.Ctx) bool

Next는 미들웨어를 건너뛰기 위한 함수를 정의합니다.

nil

Rules

map[string]string

Rules는 URL 경로 다시 쓰기 규칙을 정의합니다. 별표로 캡처된 값은 인덱스로 검색할 수 있습니다.

(Required)

Examples

package main

import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/rewrite"
)

func main() {
  app := fiber.New()

  app.Use(rewrite.New(rewrite.Config{
    Rules: map[string]string{
      "/old":   "/new",
      "/old/*": "/new/$1",
    },
  }))

  app.Get("/new", func(c *fiber.Ctx) error {
    return c.SendString("Hello, World!")
  })

  app.Get("/new/*", func(c *fiber.Ctx) error {
    return c.SendString("Wildcard: " + c.Params("*"))
  })

  app.Listen(":3000")
}

Test:

curl http://localhost:3000/old
curl http://localhost:3000/old/hello

Last updated