Node JS to Deliver HTML Files

Node JS to Deliver HTML Files: The HTTP module is used to start up HTTP servers and respond to HTTP requests from users. The fs module is used to read the file system.Node js to deliver the html file while we have express js also for making it easy as well as fast.

var http = require('http');
var fs = require('fs');
  http.createServer(function(req, res){
    fs.readFile('test.html',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });
}).listen(3000);

 console.log('Listen me at http://localhost:3000');

Another way to deliver the HTMl files using the serve-static module.

Serve-static

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Express, Node.Js Tagged with: