我尝试使用handlebars在Node JS应用程序中加载CSS文件,但收到错误消息
Refused to apply style from 'http://localhost:8080/styles/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled. 我检查了路径和它的右边。有什么解决方案吗?
在我的index.js中有app.use(express.static('public'))
和我的handlebars我有<link rel='stylesheet' type="text/css" href='/styles/main.css'>
我的工作区是
-node_modules
-public
--styles
---main.css
-template-engine
--views
---layout
----main.handlebars发布于 2020-08-23 09:32:27
这很可能是由您调用app.use(express.static('public'))的方式引起的。一般来说,最好使用下面的格式。
const path = require("path");
app.use(express.static(path.join(__dirname, "public")));不幸的是,很难从您提供的信息中猜测出问题的确切原因。您是否可以加载其他外部源,如脚本或图像?还是只有CSS文件才有这种体验?这可能是因为express找不到该文件,或者您可能需要更改文件权限。此外,这个问题在StackOverflow和其他网站上至少被问了几百次。你检查过另一个questions了吗?我敢肯定,如果这不能解决你的问题,其中一个答案将会。
最后,我建议您使用express-handlebars而不是handlebars.js。它可能更适合您的需求。别忘了检查express-handlebar中的默认文件结构。
https://stackoverflow.com/questions/63542390
复制相似问题