Express.static和CSS文件的MIME类型错误 [英] MIME type error with express.static and CSS files

查看:246
本文介绍了Express.static和CSS文件的MIME类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用express和node.在我的server.js文件中,我有这段代码:

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

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

然后在我的静态目录中,我有一个CSS文件夹,然后是一个style.css文件.在我的index.html中,我这样链接到工作表:

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:

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

但是,在我的本地主机中,我继续遇到此错误:

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

拒绝应用来自' http://localhost:3500/static/css/style的样式. css ",因为它的MIME类型('text/html')不是受支持的样式表MIME类型,并且启用了严格的MIME检查.

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.

我确定标记中的路径名是正确的,并且我对为什么收到此错误感到困惑.我需要声明将CSS文件处理为CSS而不是HTML吗?我说type="text/CSS"时不这样做吗?如果没有,我需要在服务器文件中添加些什么来解决此问题?

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'));

表示静态文件实际上是从/static提供的.在基于Unix的操作系统中,这是目录根/的直接子级.我认为您不这样做,但您不应该存储文件,更不用说该目录中的公共可访问文件了.相反,您可能正在寻找的是App目录中的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"));

发生MIME类型错误,因为送达的文件很可能是Express制作的"404未找到"页面,因为找不到文件.

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.

如果您无法在HTML中包含文件,请始终进行两次检查,甚至最好进行三重检查,以确保可以先通过浏览器访问该文件.

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.

此外,文件然后位于/...,而不是/static/...

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

这篇关于Express.static和CSS文件的MIME类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆