css文件修改后没变化 static,Express.static和CSS文件的MIME类型错误

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

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

And subsequently in my static directory, I have a CSS folder, and then a style.css file. In my index.html, I link to the sheet like so:

However, in my local host I continue to get this error:

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

I am sure that my path name in my tag is right, and I'm confused to why I'm getting this error. Do I need to declare that CSS files be processed as CSS and not HTML? Am I not doing that when I say type="text/CSS"? If not, what do I need to put into my server file to remedy this issue?

解决方案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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值