Beginning my first node.js project and I'm finding that my code renders either the html with no styling, or the css code as plain text to the screen. I open the page and see the html code, change nothing but hit refresh, and I see the css, and it switches back and forth every time I hit refresh. Can anyone tell me how to get it to apply the styling? Thanks. My code:
http.createServer(function (req, res) {
if(req.method.toLowerCase() == 'get'){
fs.readFile('path\\to\\index.html', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
//res.end();
});
}
if(req.method.toLowerCase() == 'get'){
fs.readFile('path\\to\\mystyle.css', function (err, data1) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/css'});
res.write(data1);
//res.end();
});
}
}).listen(4000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:4000/');
The reason I have the 'res.end()'s commented out is that when I left either or both of them in, I got a 'write after end' error. But I presume my problem is somewhere in that?
Edit: Just to add, I get the same problem if I remove both IF statements. If I enclose both readFiles within the same IF statement, I get the 'write after end'.