I am new at Node Js, sorry if the question is irrelevant but just wanted know that, what is the proper way of sending an HTML response to the browser. In the simple example code below I've put the server inside the readFile method is that okay, I will probably need to create a server for each file that way. It's definetly not the right way of doing it. What is the proper way of handling requests and responds in Node Js.
const http = require('http');
const fs = require('fs');
if (fs.existsSync('../Html')) {
fs.readFile('../Html/index.html', (err, data) => {
if (err) throw err;
const server = http.createServer((req, res) => {
console.log('The request is made');
res.setHeader('Content-Type', 'text/html');
res.write(data.toString());
res.end();
console.log(req.url, req.method);
});
server.listen(3000, 'localhost', () => {
console.log('Listening for the localhost:3000');
})
});
}