0

im trying to load my CSS file in my Node JS app using handlebars, but i get the error

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. 

I checked the path and its right. Any solution?

in my index.js i have app.use(express.static('public'))

and my handlebars html i have <link rel='stylesheet' type="text/css" href='/styles/main.css'>

My workspace is

-node_modules

-public
--styles
---main.css

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

1 Answer 1

3

This is most likely caused by the way you called the app.use(express.static('public')). In general it is better to use the format below.

const path = require("path");
app.use(express.static(path.join(__dirname, "public")));

Unfortunately, it is hard to guess the exact reason for the problem from the information you've provided. Can you load other external sources like scripts or images? Or do you only experience this with the CSS files? This might be caused because express cannot locate the file or you might need to change the file permissions. Furthermore, this question is asked at least hundreds of times on StackOverflow and other sites. Have you checked the other questions? I am sure if this does not solve your problem one of the answers in there will.

Finally, I suggest you use express-handlebars instead of handlebars.js. It is probably much more suited for your needs. And don't forget to check out the default file structure in the express-handlebars as well.

Sign up to request clarification or add additional context in comments.

Comments

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.