[英]How and when to strip the url to serve a static site?
I have the following directory structure for my static web site:我的静态网站有以下目录结构:
│ infinote.exe
└───spa
│ favicon.ico
│ index.html
│
├───css
│ app.f99f51d4.css
│ vendor.d9e2261d.css
│
├───fonts
│ flUhRq6tzZclQEJ-Vdg-IuiaDsNa.40fa1be9.woff
│ flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.cf9862e8.woff2
│ KFOkCnqEu92Fr1MmgVxIIzQ.9391e6e2.woff
│ KFOlCnqEu92Fr1MmEU9fBBc-.ddd11dab.woff
│ KFOlCnqEu92Fr1MmSU5fBBc-.877b9231.woff
│ KFOlCnqEu92Fr1MmWUlfBBc-.0344cc3c.woff
│ KFOlCnqEu92Fr1MmYUtfBBc-.b555d228.woff
│ KFOmCnqEu92Fr1Mu4mxM.9b78ea3b.woff
│ PatuaOne-Regular.ttf
│ Poppins-Regular.ttf
│
├───icons
│ apple-icon-120x120.png
│ apple-launch-828x1792.png
│ favicon-128x128.png
│ favicon-16x16.png
│ favicon-32x32.png
│ favicon-96x96.png
│ icon-128x128.png
│ safari-pinned-tab.svg
│
└───js
app.3a5b0240.js
vendor.a9a3886c.js
infinote.exe
is a binary compiled from Go and the line where I define how to serve the web site is infinote.exe
是从 Go 编译的二进制文件,我定义如何为网站提供服务的行是
r.Handle("/spa/", http.StripPrefix("/spa/", http.FileServer(http.Dir("./spa"))))
I use chi
as the router, and r := chi.NewRouter()
.我使用
chi
作为路由器,并且r := chi.NewRouter()
。
I expect http://example.com/spa/
to:我希望
http://example.com/spa/
能够:
http://example.com/spa/
→ this is ./spa/index.html
http://example.com/spa/
→ 这是./spa/index.html
index.html
, to request other files in ./spa
index.html
后,请求./spa
中的其他文件
http://example.com/spa/js/vendor.a9a3886c.js
→ ./spa/js/vendor.a9a3886c.js
http://example.com/spa/js/vendor.a9a3886c.js
→ ./spa/js/vendor.a9a3886c.js
What happens is that index.html
is retrieved correctly, and then all the referenced files return a 404 Not Found
.发生的情况是
index.html
被正确检索,然后所有引用的文件都返回404 Not Found
。
To be frank I do not exactly understand the mechanics (and need) of http.StripPrefix
.坦率地说,我并不完全了解
http.StripPrefix
的机制(和需要)。 Is this because the files in spa
are relative to the full URL, in other words to ./spa/spa/...
- which is not correct (and thus the need to strip app
first)?这是因为
spa
中的文件是相对于完整的 URL 的,换句话说,是相对于./spa/spa/...
- 这是不正确的(因此需要先剥离app
)?
If so, why is only index.html
retrieved correctly?如果是这样,为什么只有
index.html
正确检索? Even favicon.ico
is a 404 despite being in the same directory as index.html
.即使
favicon.ico
与index.html
位于同一目录中,也是 404。
All credit goes to @mkopriva and @Zombo whose comments were, for some reason, deleted所有功劳归于@mkopriva和@Zombo ,他们的评论由于某种原因被删除了
After several iterations and tests, I managed to find a working version:经过几次迭代和测试,我设法找到了一个工作版本:
r.Handle("/spa/*",http.StripPrefix("/spa/", ttp.FileServer(http.Dir("spa"))))
The key part was the *
in the first "pattern".关键部分是第一个“模式”中的
*
。 I did not find anything about wildcards in the documentation but with a *
it works - and without it does not (I get the 404`).我在文档中没有找到任何关于通配符的信息,但是使用
*
它可以工作——没有它就不行(我得到 404`)。
I would be delighted to have an answer that is better than that我很高兴有一个比这更好的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.