Say you have a CSS file with the code:
body{
margin:0px;
}
And the HTML file index.html. How could you create a server that takes index.html and adds the CSS? Edit:
var http = require('http');
var fs = require('fs');
function sendFile(res, filename, type) {
fs.readFile(filename, function(err, data) {
if (err) {
console.log(err);
res.statusCode = 404;
res.end();
return
;
}
res.writeHead(200, { 'Content-Type': type });
res.write(data);
res.end();
});
}
http.createServer(function(req, res) {
if (req.url == "/") {
sendFile(res, "index.html", "text/html");
}
else if (req.url == "/styles.css") {
sendFile(res, "styles.css", "text/css");
}
else {
res.statusCode = 404;
res.end();
}
}).listen(8080);
index.htmlyourself. Or, if you want to put the CSS in a separate file such asstyles.css, then you need to code your http server so that it servers bothindex.htmlandstyles.css. Since you don't show any of your web server code, we can't really help more specifically than that. Please show your existing http server code that servesindex.html.javascript var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { fs.readFile('index.html', function(err, data) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write(data); return res.end(); }); }).listen(8080);