Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR HTML

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);
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #reading #html #file #writing #nodejs
ADD COMMENT
Topic
Name
2+7 =