reading html file and writing response.write nodejs code example

Example: reading html file and writing response.write nodejs

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;
    } else if(html){
    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);
});

Tags:

Misc Example