css - 尝试将 .css 链接到 .ejs 文件时出现错误

标签 css node.js express ejs

我正在使用 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/

相关文章:

jQuery 选项卡启用水平滚动

Node.js,如何避免事件命名空间冲突?

node.js - 递归执行 promise nodejs

express - Mocha + super 测试 + 断言 : print response body on test failure

node.js - Mongoose 如何根据自定义字段更新(如果存在)。否则插入

html - 无法更改图像大小

css 3d立方体在悬停旋转时有奇怪的空间

html - Css 在压缩模式下不工作

node.js - react npm start 中的 BROWSER 环境变量不选择自定义浏览器

node.js 回调、嵌套函数、如何重构