cloudwego / hertz

Go HTTP framework with high-performance and strong-extensibility for building micro-services.
https://www.cloudwego.io
Apache License 2.0
5.12k stars 496 forks source link

路由问题 #1097

Closed namecost closed 4 months ago

namecost commented 4 months ago

当没有定义相关路由时,返回下面的错误: Cannot open requested path

{"level":"error","message":"HERTZ: Cannot open file=\"./static/ghfghfgh/ind8ex.html\", error=open ./static/ghfghfgh/ind8ex.html: no such file or directory"}

li-jin-gou commented 4 months ago

没看懂要问啥🤨

namecost commented 4 months ago

没看懂要问啥🤨

没有定义相关路由的时候,系统会自动寻找相关静态文件。

li-jin-gou commented 4 months ago

代码咋写的发下

namecost commented 4 months ago

func GeneratedRegister(r server.Hertz) { //INSERT_POINT: DO NOT DELETE THIS LINE! r.NoRoute(func(c context.Context, ctx app.RequestContext) { //ctx.NotFound() ctx.Redirect(consts.StatusFound, []byte("/404")) // 未匹配的跳转到404 return // Status Code: 404 }) // set NoMethod handler r.NoMethod(func(c context.Context, ctx *app.RequestContext) { ctx.NotFound() return // Status Code: 404 }) r.StaticFile("/favicon.ico", "./static/favicon.ico") root := r.Group("/", rootMw()...) { root.GET("/404", append(f404Mw(), f404.AddInfo)...) root.GET("/", append(rootMw(), home.AddInfo)...) root.GET("/page/:id/", append(rootMw(), home.AddInfo)...)

    _page := root.Group("/service", _pageMw()...)
    _page.GET("/register.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/search.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/about.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/privacy.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/contact.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/dmca.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/advertise.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/buy.:id", append(_pageMw(), page.AddInfo)...)
    _page.GET("/buy-success.:id", append(_pageMw(), page.ResultInfo)...)
    _page.GET("/buy-error.:id", append(_pageMw(), page.ResultInfo)...)
    _page.GET("/submit-error.:id", append(_pageMw(), page.ResultInfo)...)

    _page.POST("/register.:id", append(_pageMw(), page.RegisterPost)...)
    _page.POST("/search.:id", append(_searchMw(), page.SearchPost)...)
    _page.POST("/contact.:id", append(_pageMw(), page.ContactPost)...)
    _page.POST("/dmca.:id", append(_pageMw(), page.DMCAPost)...)
    _page.POST("/submit.:id", append(_pageMw(), page.SubmitPost)...)
    _page.POST("/form.:id", append(_pageMw(), page.FormPost)...)

    root.GET("/:name.html", append(_postMw(), post.AddInfo)...)

    _sitemap := root.Group("/sitemap.:id", _sitemapMw()...)
    _sitemap.GET("/", append(_sitemapMw(), sitemap.AddInfo)...)
    _sitemap.GET("/:id/", append(_sitemapMw(), sitemap.AddInfo)...)

    _tag := root.Group("/:name", _tagMw()...)
    _tag.GET("/", append(_tagMw(), tag.AddInfo)...)
    _tag.GET("/:id/", append(_tagMw(), tag.AddInfo)...)

}

}

namecost commented 4 months ago

访问这个页面 /ghfghfgh/ind8ex.html 就会出现 Cannot open requested path

{"level":"error","message":"HERTZ: Cannot open file=\"./static/ghfghfgh/ind8ex\", error=open ./static/ghfghfgh/ind8ex: no such file or directory"} {"level":"info","t":1713709033,"m":"GET","s":404,"ip":"19.2.49.22","h":"test2.twzo.com","p":"/ghfghfgh/ind8ex","ua":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36","r":""}

li-jin-gou commented 4 months ago

image 检查一下是不是注册 StaticFS 和 Static 的路由就会这样,变成通配路由

namecost commented 4 months ago

render里面加了这个,注释掉就好了。 //h.Static("/", "./static")

github-actions[bot] commented 4 months ago

This issue has been marked as invalid question, please give more information by following the issue template. The issue will be closed in 1 days if no further activity occurs.

namecost commented 4 months ago

有没有其他解决方案,直接当找不到文件的时候直接返回404,而不是给出这个“Cannot open requested path”

github-actions[bot] commented 4 months ago

This issue has been marked as invalid question, please give more information by following the issue template. The issue will be closed in 1 days if no further activity occurs.