页面CSS不会加载 - 简单的html和js文件

时间:2017-08-06 17:07:16

标签: css node.js

我正在创建一个简单的页面来开始学习HTML,CSS和java脚本。我有我的HTML文件,其中包含以下指向我的CSS文件的链接,但由于某些原因,没有任何CSS加载。

我通过节点在localhost上运行它。

html:

<!DOCTYPE html>
<html>
<head>
  <link href='./css/styles.css' type='text/css' rel='stylesheet'>
</head>

我的server.js文件:

var http = require('http');
var fs = require('fs');

const PORT=8080;

fs.readFile('./index.html', function (err, html) {

    if (err) throw err;

    http.createServer(function(request, response) {
        response.writeHeader(200, {"Content-Type": "text/html"});
        response.write(html);
        response.end();
    }).listen(PORT);
});

我也得到了错误

  

“资源解释为样式表,但使用MIME类型传输   text / html:“http://localhost:8080/css/styles.css”。“

我是否需要在“server.js”文件中添加“text / css”的其他内容类型?

文档树:

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以在此html页面外部添加脚本。它可以位于head标记内部,就像CSS定义一样。

<script type="text/javascript" src="../Script_folder/server.js"></script> 

要小心到达树中的文件。 ../表示一个级别向上,~/指向root个节点。所以,我想知道您的路由在CSS地址中只有一个.

<link href='../css/styles.css' type='text/css' rel='stylesheet'> 有两个点。 ../

答案 1 :(得分:0)

在href属性中,您只使用了一个点而不是两个点。你的href应该像

  <link href='../css/styles.css' type='text/css' rel='stylesheet'>
相关问题