Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

路由问题 #1097

Closed
namecost opened this issue Apr 21, 2024 · 10 comments
Closed

路由问题 #1097

namecost opened this issue Apr 21, 2024 · 10 comments
Labels
invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) stale

Comments

@namecost
Copy link

当没有定义相关路由时,返回下面的错误:
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"}

@github-actions github-actions bot added the invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) label Apr 21, 2024
@li-jin-gou
Copy link
Member

没看懂要问啥🤨

@namecost
Copy link
Author

没看懂要问啥🤨

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

@li-jin-gou
Copy link
Member

代码咋写的发下

@namecost
Copy link
Author

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
Copy link
Author

访问这个页面 /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
Copy link
Member

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

@namecost
Copy link
Author

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

Copy link

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.

@github-actions github-actions bot added the stale label Apr 21, 2024
@namecost
Copy link
Author

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

@github-actions github-actions bot removed the stale label Apr 22, 2024
Copy link

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.

@github-actions github-actions bot added the stale label Apr 22, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid issue invalid issue (not related to Hertz or described in document or not enough information provided) stale
Development

No branches or pull requests

2 participants