【发布时间】:2018-12-14 13:25:40
【问题描述】:
我将使用此节点服务器为静态index.html 和main.css 提供服务:
serve.js:
var express = require('express')
var cors = require('cors')
var app = express()
var path = require('path');
app.use(cors())
app.use(express.static('assets'))
app.get('/', function (req, res, next) {
res.sendFile(path.join(__dirname + '/index.html'));
})
app.listen(3000, function () {
console.log('CORS-enabled web server listening on port 3000')
})
index.html:
<!doctype html>
<html class="no-js" lang="">
<head>
<link rel="stylesheet" type="text/css" href="./assets/css/main.css">
</head>
<body>
<p>Hello Html!</p>
<div class="thejson"></div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</body>
</html>
main.css
body {
background: #ffdd;
color: #eaeaea;
padding: 10px;
}
结构:
project structure:
index.html
serve.js
assets
js
css
main.css
当我加载 index.html 时,css 已加载,但从节点提供它,我得到:
Refused to apply style from 'http://127.0.0.1:3000/assets/css/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
我也尝试过href="/assets/css/main.css" 和href="./assets/css/main.css",但无济于事。
这里可能有什么问题?我该如何解决?
【问题讨论】:
-
@zer00ne 不确定该答案如何适用于我的情况,但将 css 文件名更改为
default.css不起作用。 -
您应该阅读它,Codisan 显然做到了。