Rewrite

原文: https://docs.gofiber.io/api/middleware/rewrite

Rewrite 重写

Rewrite middleware rewrites the URL path based on provided rules. It can be helpful for backward compatibility or just creating cleaner and more descriptive links.

​ 重写中间件根据提供的规则重写 URL 路径。它有助于向后兼容或只是创建更简洁、更具描述性的链接。

Signatures 签名

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

Config 配置

Property 属性Type 输入Description 说明Default 默认
Next 下一步func(*fiber.Ctx) boolNext defines a function to skip middleware. 接下来定义一个跳过中间件的函数。nil
Rules 规则map[string]stringRules defines the URL path rewrite rules. The values captured in asterisk can be retrieved by index. 规则定义 URL 路径重写规则。星号中捕获的值可以通过索引检索。(Required) (必需)

Examples 示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
最后修改 October 10, 2024: 更新 (a4b8f85)