lord
lord

Reputation: 59

Reading css file from nodejs

How do I get my css file to be read by node js. I have the html file read by the code below.

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

Upvotes: 1

Views: 8413

Answers (1)

Gildas.Tambo
Gildas.Tambo

Reputation: 22643

The same way you can do

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();
});

Upvotes: 4

Related Questions