我正在使用 ejs 处理 node.js(express),但我无法向其包含 .css 文件 它的浏览器显示以下错误。
localhost/:1 Refused to apply style from
'http://localhost:3000/posts/app.css' because its MIME type
('text/html') is not a supported stylesheet MIME type, and strict MIME
checking is enabled.
我的 app.js 是这样的:
var express = require('express');
var app = express();
app.use(express.static("public"));
app.get("/",function(req,res){
res.render("home.ejs");
});
app.get("/posts",function(req,res){
var posts = [
{title : "Post 1",author : "Malinda"},
{title : "Hello Sri Lankda",author : "Supun"},
{title : "Hello World",author : "Gokula"}
]
res.render("posts.ejs",{posts : posts});
});
app.listen(3000,function(){
console.log("Server Started");
});
和 posts.ejs 文件
<link rel="stylesheet" type="text/css" href="app.css">
<h1>The Post Page</h1>
<p>Using For Loop</p>
<% for(var i =0; i < posts.length; i++){ %>
<li>
<%= posts[i].title%> - <strong><%= posts[i].author%></strong>
</li>
<% } %>
<br><br>
<p>Using For Each Loop</p>
<% posts.forEach(function(post){ %>
<li>
<%= post.title%> - <strong><%= post.author%></strong>
</li>
<% }) %>
和css文件
body{
background : yellow;
color : red;
}
请大家帮我弄清楚哪里出了问题
最佳答案
检查这个Node/Express - Refused to apply style because its MIME type ('text/html') 似乎是同样的问题。尝试删除
之后的多余空格app.css
可能是您的文本编辑器没有检测到它。
关于css - 尝试将 .css 链接到 .ejs 文件时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49595938/