I am new to node.js and js in general. I have a simple app that have a styles.css but I dont know how to link it on the app.js.
In the index.html it has <link rel="stylesheet" type="text/css" href="css/styles.css">
And this is my app.js for the node.js:
const http = require('http');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
fs.readFile('index.html', (err, html) =>{
if (err){
throw err;
}
const server = http.createServer((req,res) => {
res.statusCode = 200;
res.setHeader('Content-type', 'text/html');
res.write(html);
res.end();
});
server.listen(port, hostname, () => {
console.log('Server started on port ' + port);
});
});
So how should I modify app.js so that it will be able to locate the css file?
index.html. So when the browser parses the html, it then requestscss/styles.csswhich your server responds by sending it theindex.htmlagain. You should distinguish between requests forindex.htmlandcss/styles.cssto return content accordingly.