1

I have a node.js app with a static css file. When I create a middleware and call the css file, it is geeting an error as follows:

Refused to apply style from 'http://localhost:3000/files/style.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.

Here is my app.js file

var express = require ('express');

var app = express();

app.set('view engine', 'ejs');
app.use('/files', express.static('/files'));

app.get('/', function(req, res){
    res.render('index');
});

app.listen(3000);

index.ejs

<!DOCTYPE html>
<html>
<head>
    <title>New page</title>
    <link rel="stylesheet" type="text/css" href="files/style.css">
</head>
<body>
    <p>New style</p>
</body>
</html>

When I use URL : http://localhost:3000/files/style.css

Display "Cannot GET /files/style.css" and when checking in the console's network tab, it says styles.css not found. This happens when I add static javascript files too.

I also tried this with follows

app.use('/files', express.static(__dirname + '/files'));
app.use(express.static(__dirname + '/files'));
app.use(express.static('files'));

and none of them has worked so far.

How can I solve this?

1 Answer 1

2

You try to get file from files/files/files/style.css

Try code below:

NodeJs:

app.use(express.static(path.join(__dirname, 'files')));

HTML:

<link rel="stylesheet" type="text/css" href="/style.css">
Sign up to request clarification or add additional context in comments.

3 Comments

@David Johns I created worked example on github. Please check it. I hope it will help you
Thank you very much
Hey, I did that but I got a problem in live, I had host this with apache without the port specified in server.js For this I am using .htaccess to forward it to specified url, that gives 404 for the static files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.