CSS file blocked: MIME type mismatch (X-Content-Type-Options: nosniff)

后端 未结 9 2090
失恋的感觉
失恋的感觉 2020-12-08 13:21

I am developing an Angular 4 app and I want to apply some global styles. Following the tutorial at the angular site, I\'ve created a \"styles.css\" file in the root directo

相关标签:
9条回答
  • 2020-12-08 13:58

    What seemed to work for me was changing

    <script type="text/javascript" src="/lib/matrix.js"></script>
    

    to

    <script type="text/javascript" src="./lib/matrix.js"></script>
    
    0 讨论(0)
  • 2020-12-08 14:02

    In my case I had jumbled up the code blocks below in reverse order so I was getting this error. If you have this issue, follow the below order and it might help

    1.

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

    2.

    app.get('*', (req, res) => {
       res.sendFile(path.join(__dirname, 'public/index.html'));
    });
    

    3.

    app.use('/api', cors(corsOptions), indexRouter);
    
    0 讨论(0)
  • 2020-12-08 14:07

    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.

    0 讨论(0)
提交回复
热议问题