0

Im working on node.js(express) with ejs and im not able to include a .css file to it It browser shows the following error.

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.

My app.js goes thus:

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");
});

and posts.ejs file

<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>
<% }) %>

and css file

body{
  background : yellow;
  color : red;
}

please any one heip me to figure out what went wrong

Malinda
  • 524
  • 2
  • 5
  • 11

3 Answers3

1

check this Node/Express - Refused to apply style because its MIME type ('text/html') seems like the same problem. Try to Remove the extra spaces after

app.css

propably your text editor didnt detect it.

1

In link tag in post.ejs use href="/app.css".you have to use "/" there

Malinda
  • 524
  • 2
  • 5
  • 11
0

The resource app.css is static and hence needs to be specified in the server setting.

Write this before app.listen - app.use('/css', express.static('css'));

Here CSS is a folder on your root, now you'd be able to get all the CSS files in that folder.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Emmanual
  • 124
  • 1
  • 8