英文:
How to prevent Fiber from auto registerting HEAD routes?
问题
Fiber v2(https://gofiber.io/)会自动为每个GET路由添加一个HEAD路由。
有没有办法阻止这个行为?
我只想注册GET路由。实际上,我只想注册我明确添加的那些路由。
这个能做到吗?
英文:
Fiber v2 (https://gofiber.io/) automatically adds a HEAD route for each GET route.
Is it possible to prevent this?
I want only the GETs to be registered. Actually, I want to register only those routes I add explicitly.
Is it possible to do this?
答案1
得分: 0
查看(*App).Get的实现:
// Get为GET方法注册一个路由,该方法请求指定资源的表示形式。使用GET的请求应仅检索数据。
func (app *App) Get(path string, handlers ...Handler) Router {
return app.Head(path, handlers...).Add(MethodGet, path, handlers...)
}
以及(*Group).Get的实现:
// Get为GET方法注册一个路由,该方法请求指定资源的表示形式。使用GET的请求应仅检索数据。
func (grp *Group) Get(path string, handlers ...Handler) Router {
grp.Add(MethodHead, path, handlers...)
return grp.Add(MethodGet, path, handlers...)
}
无法阻止这种行为。您只能避免使用它们,并直接使用Add
方法。例如,像这样注册一个GET
路由:
app.Add(fiber.MethodGet, "/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
请注意,(*App).Use和(*Group).Use匹配所有HTTP动词。您可以像这样删除HEAD
方法:
methods := make([]string, 0, len(fiber.DefaultMethods)-1)
for _, m := range fiber.DefaultMethods {
if m != fiber.MethodHead {
methods = append(methods, m)
}
}
app := fiber.New(fiber.Config{
RequestMethods: methods,
})
注意:每当注册一个HEAD
路由时,它会引发panic,因为它不包含在RequestMethods
中。
我不知道您为什么要这样做。也许更好的选择是使用一个中间件来拒绝所有的HEAD
请求,像这样:
app.Use(func(c *fiber.Ctx) error {
if c.Method() == fiber.MethodHead {
c.Status(fiber.StatusMethodNotAllowed)
return nil
}
return c.Next()
})
英文:
See the implementation of (*App).Get:
// Get registers a route for GET methods that requests a representation
// of the specified resource. Requests using GET should only retrieve data.
func (app *App) Get(path string, handlers ...Handler) Router {
return app.Head(path, handlers...).Add(MethodGet, path, handlers...)
}
And (*Group).Get:
// Get registers a route for GET methods that requests a representation
// of the specified resource. Requests using GET should only retrieve data.
func (grp *Group) Get(path string, handlers ...Handler) Router {
grp.Add(MethodHead, path, handlers...)
return grp.Add(MethodGet, path, handlers...)
}
There is no way to prevent this behavior. All you can do is to avoid using them and use the Add
method directly. For example, register a GET
route like this:
app.Add(fiber.MethodGet, "/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
Please note that (*App).Use and (*Group).Use match all HTTP verbs. You can remove the HEAD
method like this:
methods := make([]string, 0, len(fiber.DefaultMethods)-1)
for _, m := range fiber.DefaultMethods {
if m != fiber.MethodHead {
methods = append(methods, m)
}
}
app := fiber.New(fiber.Config{
RequestMethods: methods,
})
Note: it panics whenever a HEAD
route is registered since it's not included in RequestMethods
.
I don't know why you want to do this. Maybe a better choice is to use a middleware to reject all HEAD
requests like this:
app.Use(func(c *fiber.Ctx) error {
if c.Method() == fiber.MethodHead {
c.Status(fiber.StatusMethodNotAllowed)
return nil
}
return c.Next()
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论