Reputation: 59
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
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