英文:
CSS not rendered in Go server
问题
我正在使用chi
作为路由器。
我的文件树如下:
-cmd
- web
- main.go和其他一些go文件*
- static
- assets
- 这里有一些html文件*
我已经设置好了我的路由:
mux := chi.NewRouter()
mux.Use(middleware.Logger)
mux.Use(middleware.SetHeader("Cache-Control", "no-store"))
mux.Get("/about", app.serveFile)
mux.Get("/login", app.serveFile)
mux.Get("/register", app.serveFile)
mux.Get("/", app.serveFile)
fileServer := http.FileServer(http.Dir("./cmd/web/static/assets/"))
mux.Handle("/assets/", http.StripPrefix("/assets/", fileServer))
这是我的index.html文件的一部分:
<link rel="shortcut icon" href="/assets/images/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/assets/css/style.css">
编辑1:
我正在使用make
作为我的构建工具。这是构建和运行web的规则:
build_web:
@echo "building projectName web..."
@go build -o ${BINARY_DIR}/${WEB_BINARY_NAME} ./cmd/web/*.go
@echo "projectName web built"
run_web:
@echo "running projectName web..."
@./${BINARY_DIR}/${WEB_BINARY_NAME} -environment ${PREF_ENV} -port ${WEB_PORT}
在本地主机上运行服务器时,HTML页面被渲染,但CSS样式没有应用。我检查了开发者工具的网络选项卡,显示所有资源(CSS/JS/图像)的状态码为200。中间件日志记录器也报告了同样的情况,资产文件夹中的所有文件状态码都是200。
我尝试了我能找到的所有stackoverflow答案,但仍然没有运气。
英文:
I'm using chi
as router.
My files tree is:
-cmd
- web
- main.go and some other go files here*
- static
- assets
- some html files here*
I have my routes set up like so:
mux := chi.NewRouter()
mux.Use(middleware.Logger)
mux.Use(middleware.SetHeader("Cache-Control", "no-store"))
mux.Get("/about", app.serveFile)
mux.Get("/login", app.serveFile)
mux.Get("/register", app.serveFile)
mux.Get("/", app.serveFile)
fileServer := http.FileServer(http.Dir("./cmd/web/static/assets/"))
mux.Handle("/assets/", http.StripPrefix("/assets/", fileServer))
Here is a snippet of my index.html file:
<link rel="shortcut icon" href="/assets/images/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="/assets/css/style.css">
Edit 1:
I'm using make
as my build tool. Here is the rule to build and run the web:
build_web:
@echo "building projectName web..."
@go build -o ${BINARY_DIR}/${WEB_BINARY_NAME} ./cmd/web/*.go
@echo "projectName web built"
run_web:
@echo "running projectName web..."
@./${BINARY_DIR}/${WEB_BINARY_NAME} -environment ${PREF_ENV} -port ${WEB_PORT}
Running the server on localhost, HTML pages are rendered but css rules are not applied. I checked the network tab of developer tools and it shows status code 200 for all assets (css/js/images). Same was reported through the middleware logger, a 200 status code for all files in the assets folder.
I've tried all stackoverflow answers I could stumble upon, still no luck.
答案1
得分: 1
我最终是如何解决这个问题的:
我在浏览器中输入<address>/assets/css/style.css
,返回了404页面未找到的错误。
尽管服务器日志显示状态码为200,但实际上文件并没有被提供。所以我将文件服务器上的路径从/assets/
改为/assets/*
(这里是mux.Handle("/assets/*", http.StripPrefix("/assets/", fileServer))
),然后问题就解决了。
感谢@bayta-darrel和@mkopriva的帮助。
英文:
How I eventually fixed this:
I entered <address>/assets/css/style.css
in my browser and it returned 404 not found page.
Though it shows a status code 200 in the server log, but the file was not actually served. So I changed the path on my fileserver from /assets/
to /assets/*
(here: mux.Handle("/assets/*", http.StripPrefix("/assets/", fileServer))
) and it worked,
Thanks to @bayta-darrel and @mkopriva for their help
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论