【问题标题】:Reading css file from nodejs从nodejs读取css文件
【发布时间】:2016-06-11 03:47:27
【问题描述】:

如何让节点 js 读取我的 css 文件。我有下面代码读取的 html 文件。

function displayForm(res) {
fs.readFile('index.html', function (err, data) {
    res.writeHead(200, {
        'Content-Type': 'text/html',
    });
    res.write(data);
    res.end();
});
 }

【问题讨论】:

    标签: html css node.js


    【解决方案1】:

    你可以这样做

    html

    fs.readFile(__dirname + '/public/index.html', function (err, data) {
      if (err) console.log(err);
      res.writeHead(200, {'Content-Type': 'text/html'});
      res.write(data);
      res.end();
    });
    

    js

    fs.readFile(__dirname + '/public/js/script.js', function (err, data) {
      if (err) console.log(err);
      res.writeHead(200, {'Content-Type': 'text/javascript'});
      res.write(data);
      res.end();
    });
    

    css

    fs.readFile(__dirname + '/public/css/style.css', function (err, data) {
      if (err) console.log(err);
      res.writeHead(200, {'Content-Type': 'text/css'});
      res.write(data);
      res.end();
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-09
      • 2019-03-30
      • 2020-08-08
      • 2015-05-04
      相关资源
      最近更新 更多