静态文件是通过`.Handle()`方法进行服务的。

huangapple go评论79阅读模式
英文:

How are static files served through .Handle() targeted?

问题

我对我的应用程序提供的静态网站文件的检索方式感到困惑。

我有一个典型的代码:

r.Handle("/", http.FileServer(http.Dir("spa")))

spa文件夹的内容如下:

│   favicon.ico
│   index.html
│
├───css
│       510.e095f672.css
│       app.f05c50d3.css
│       vendor.9add3052.css
│
├───fonts
│       flUhRq6tzZclQEJ-Vdg-IuiaDsNa.1dd1bb36.woff
│       flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.f54bbe10.woff2
│       KFOkCnqEu92Fr1MmgVxIIzQ.9391e6e2.woff
│       KFOlCnqEu92Fr1MmEU9fBBc-.ddd11dab.woff
│       KFOlCnqEu92Fr1MmSU5fBBc-.877b9231.woff
│       KFOlCnqEu92Fr1MmWUlfBBc-.0344cc3c.woff
│       KFOlCnqEu92Fr1MmYUtfBBc-.b555d228.woff
│       KFOmCnqEu92Fr1Mu4mxM.9b78ea3b.woff
│
├───icons
│       favicon-128x128.png
│       favicon-16x16.png
│       favicon-32x32.png
│       favicon-96x96.png
│
└───js
        361.136e16c3.js
        510.546967b6.js
        898.3690f332.js
        997.92f440f8.js
        app.42bde279.js
        app.578482b2.js
        vendor.8c20bf3b.js

当访问http://localhost:15555/时,我的日志显示:

2022-03-31T20:20:10+02:00 | INFO  | / → [::1]:23750
2022-03-31T20:20:10+02:00 | INFO  | /js/vendor.8c20bf3b.js → [::1]:23750
2022-03-31T20:20:10+02:00 | INFO  | /js/app.42bde279.js → [::1]:23751
2022-03-31T20:20:10+02:00 | INFO  | /css/vendor.9add3052.css → [::1]:23752
2022-03-31T20:20:10+02:00 | INFO  | /css/app.f05c50d3.css → [::1]:23753

只有第一个调用成功(状态码200),其他的都是404

第一个调用最终会检索index.html,而且确实成功了。这就是为什么会访问链接的.js.css文件。

然而,尝试访问http://localhost:15555/时,我也在检索中得到了404的错误:

2022-03-31T20:26:58+02:00 | INFO  | /index.html → [::1]:24233

我的问题:

  • 为什么/成功了,最终得到了/index.html
  • 其他文件为什么是404?
英文:

EDIT: I am usinng chi as the router. It works differently from the standard one. See the answer for the key difference, relevant to my question.

I am confused about how the files from my static site served by my application are targeted for retrieval.

I have a typical

r.Handle("/", http.FileServer(http.Dir("spa")))

and the content of spa is

│   favicon.ico
│   index.html
│
├───css
│       510.e095f672.css
│       app.f05c50d3.css
│       vendor.9add3052.css
│
├───fonts
│       flUhRq6tzZclQEJ-Vdg-IuiaDsNa.1dd1bb36.woff
│       flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.f54bbe10.woff2
│       KFOkCnqEu92Fr1MmgVxIIzQ.9391e6e2.woff
│       KFOlCnqEu92Fr1MmEU9fBBc-.ddd11dab.woff
│       KFOlCnqEu92Fr1MmSU5fBBc-.877b9231.woff
│       KFOlCnqEu92Fr1MmWUlfBBc-.0344cc3c.woff
│       KFOlCnqEu92Fr1MmYUtfBBc-.b555d228.woff
│       KFOmCnqEu92Fr1Mu4mxM.9b78ea3b.woff
│
├───icons
│       favicon-128x128.png
│       favicon-16x16.png
│       favicon-32x32.png
│       favicon-96x96.png
│
└───js
        361.136e16c3.js
        510.546967b6.js
        898.3690f332.js
        997.92f440f8.js
        app.42bde279.js
        app.578482b2.js
        vendor.8c20bf3b.js

When accessing http://localhost:15555/ I get in my logs

2022-03-31T20:20:10+02:00 | INFO  | / → [::1]:23750
2022-03-31T20:20:10+02:00 | INFO  | /js/vendor.8c20bf3b.js → [::1]:23750
2022-03-31T20:20:10+02:00 | INFO  | /js/app.42bde279.js → [::1]:23751
2022-03-31T20:20:10+02:00 | INFO  | /css/vendor.9add3052.css → [::1]:23752
2022-03-31T20:20:10+02:00 | INFO  | /css/app.f05c50d3.css → [::1]:23753

Only the first call is successful (200), the others are 404

This first call is finally supposed to retrieve index.html, and it does. This is why the linked .js and .css files are accessed.

This said, trying http://localhost:15555/ I also get a 404 on the retrieval

2022-03-31T20:26:58+02:00 | INFO  | /index.html → [::1]:24233

My questions:

  • why does / succeed as it ultimately gets /index.html?

  • why are other files 404?

答案1

得分: 2

我找到了问题,处理程序必须是

r.Handle("/*", http.FileServer(http.Dir("spa")))

我以为/会匹配所有内容,不需要通配符。

英文:

I found the problem, the handler must be

r.Handle("/*", http.FileServer(http.Dir("spa")))

I thought that / would match everything and that a wildcard was not needed.

huangapple
  • 本文由 发表于 2022年4月1日 02:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/71697617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定