MIME type error with express.static and CSS files

前端 未结 3 1972
温柔的废话
温柔的废话 2021-01-12 05:48

I\'m using express and node. In my server.js file I have this piece of code:

app.use(express.static(\'/static\'));

And subsequ

相关标签:
3条回答
  • 2021-01-12 06:29

    In my case it was what Luca Kiebel said:

    The MIME Type Error is happening because the file served is likely a "404 Not Found" page made by Express, because it couldn't locate the file.

    Just click the link from the debug Console in your browser and see what it actually returns.

    0 讨论(0)
  • 2021-01-12 06:35

    I had the same error, but it was caused by a not machting case. I wrote in my HTML file:

    <script src = "./myScript.js"></script>
    

    But the actual filename was MYSCRIPT.js. I didn't noticed that, because when I open the HTML file locally, browsers seem to ignore this and import even files where the cases not match.

    0 讨论(0)
  • 2021-01-12 06:43
    app.use(express.static('/static'));
    

    means that the static files are served literally from /static. In a Unix-Based Operating System, this is a direct child to the directory root /. I don't think you do, but you shouldn't store files, let alone publicly accessible files in that directory. Instead what you are probably looking for is the static directory in your App's directory.

    This is how you can tell express to use that one to server files:

    app.use(express.static(__dirname + "/static"));
    

    The MIME Type Error is happening because the file served is likely a "404 Not Found" page made by Express, because it couldn't locate the file.

    If you can't include a file in your HTML always double, or better even tripple check, that the file can be accessed through the browser first.

    Also, the files are then located in /..., not /static/...

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