【问题标题】:Problems getting CSS in Node JS在 Node JS 中获取 CSS 的问题
【发布时间】:2020-12-11 23:49:57
【问题描述】:

我试图使用把手在我的 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'))

我的车把 html 我有 <link rel='stylesheet' type="text/css" href='/styles/main.css'>

我的工作区是

-node_modules

-public
--styles
---main.css

-template-engine
--views
---layout
----main.handlebars

【问题讨论】:

标签: javascript html css node.js handlebars.js


【解决方案1】:

这很可能是由您调用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-handlebars 中的默认文件结构。

【讨论】:

    猜你喜欢
    • 2020-09-23
    • 2011-08-29
    • 2019-07-08
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多