CSS File Blocked: MIME Type Mismatch (X-Content-Type-Options: Nosniff)


Answer :

I just ran into the same issue. It appears to be a quirk of Express that can manifest itself for a few different reasons, judging by the number of hits from searching the web for "nodejs express css mime type".



Despite the type="text/css" attribute we put in our <link elements, Express is returning the CSS file as



Content-Type: "text/html; charset=utf-8"


whereas it really should be returning it as



Content-Type: "text/css"


For me, the quick and dirty workaround was to simply remove the rel= attribute, i.e., change



<link rel="stylesheet" type="text/css" href="styles.css">


to



<link type="text/css" href="styles.css">


Testing confirmed that the CSS file was downloaded and the styles did actually work, and that was good enough for my purposes.



I also removed rel = "stylesheet", and I no longer get the MIME type error, but the styles are not being loaded



Some answers suggested removing rel="stylesheet", that didn't work out for me however.
According to the expressjs documentation: https://expressjs.com/en/starter/static-files.html
use express.static function to serve static files such as CSS, JavaScript,etc...



app.use(express.static('public'))


and from there you should be able to load any file under the public directory
for example, if you have a style.css file inside the directory {PROJECT_PATH}/public/css/



http://localhost:3000/css/style.css will work.



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android SDK Location Should Not Contain Whitespace, As This Cause Problems With NDK Tools