【发布时间】:2018-02-24 16:26:37
【问题描述】:
我是 NodeJS 的初学者,在加载 CSS 时遇到问题。这是我的代码。
我只是创建一个在9090 端口运行的服务器并加载默认的HTML 文件。
var server = http.createServer(function(request, response) {
var html = fs.readFileSync('./FirstApp/HtmlPages/index.html');
response.writeHead(200,{"Content-Type": "text/html"});
response.write(html);
response.end();
});
server.listen(9090);
在加载http://localhost:9090/ 时,我可以看到index.html html 页面,但看不到链接的css 功能。(如果我只是在浏览器中加载我的index.html,我就能看到css功能,但不通过服务器)
这是我的简单 HTML。
<html>
<head>
<link type="text/css" rel="stylesheet" href="./css/styles.css">
</head>
<body>
<h2 class="heading"><em>Login Page</em></h2>
</body>
</html>
CSS文件
.heading {
text-align: center;
}
我可以在浏览器控制台中看到以下警告
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:9090/css/styles.css".
任何帮助将不胜感激
【问题讨论】:
-
确保您将 css 链接到正确的路径
-
我觉得 css 文件的路径是正确的,因为如果我点击 VSCode 中的路径,我可以导航到 css 文件