Hello World with Node.js

Posted on in JavaScript Node JS

See the example below for an easy Hello World example with Node.js. Create a Node.JS server and serve a hardcoded response, with content-type: ?text/html?. And listen to localhost:8000 in your browser to see the result. In your project folder create an app.js file:

var http = require('http');
http.createServer(function(req, res){ 
   var html = BoilerplateHello World"; 
   res.writeHead(200, { 
      'Content-Type' : 'text/html', 'Content-Length' : html.length 
   }); 
   res.end(html);
}).listen(8000,'127.0.0.1');

With the above example you can?t create a real-life application. Let?s create an MVC folder structure in your project folder. By creating the following folders: ?controller?, ?model?, ?views?. In the ?views? directory, create here an index.html file. With the following content:

Then change your app.js file like the example below to retrieve the index.html file from the views folder:

var http = require('http');
var path = require('path');
var fs = require('fs');
http.createServer(function(req, res){ 
   var filename = path.basename(req.url) || 'index.html', 
   ext = path.extname(filename), 
   localPath = __dirname + "/views/"; 
   //console.log(localPath); 

   if(ext == ".html"){ 
      localPath += filename; 
      path.exists(localPath, function(exists){ 
         if(exists){ 
            getFile(localPath, res) 
         } else { 
            res.writeHead(404); 
            res.end(); 
         } 
       }); 
    }
}).listen(8000, '127.0.0.1'); 
getFile = function(localPath,res){ 
   fs.readFile(localPath, function(err, contents){ 
      if(!err){ 
         res.end(contents); 
      } else { 
         res.writeHead(500); 
         res.end(); 
      } 
});

what is levitra cialis professional cialis coupons printable viagra dose online viagra does the government fund viagra levitra coupon cost of levitra buy viagra generic for viagra cialis coupons from manufacturer what is cialis cialis otc viagra patent expiration discount viagra cialis 20mg buy levitra online viagra 100mg price walmart inexpensive viagra pills levitra vs cialis cost of viagra natural viagra over the counter viagra generic for cialis utilisation viagra where to buy viagra cialis prices cyalis healthy man viagra viagra without a doctor prescription viagra price 9 levitra at walmart levitra canada sildenafil vs viagra how long does levitra last viagra alternatives cialis 5mg daily levitra vs viagra cialis 20 mg viagra vs cialis vs levitra viagra prices without insurance women viagra cialis coupons 2017 cialis vs viagra canadian viagra levitra 20mg cialis without a doctor's prescription levitra reviews cialis coupons viagra pill viagra canada non prescribed viagra viagra best price liquid cialis price cialis viagra from amazon how much does cialis cost cialis sample cialis 10 mg viagra coupons 75 off cialis pricing cialis side effects cialis generic viagra side effects best price viagra cialis patent expiration 2017 cialis or viagra trial viagra sample cealis cialis generic tadalafil cheap cialis generic viagra viagra vs cialis side effects of cialis cialis canada female viagra pills cialis free trial cialis on line viagra on line viagra samples erectile dysfunction cialis cialis website canada viagra sex viagra for women buy cialis online cilias cialis cost sophia viagra how long does viagra last cialis cheap stendra vs viagra cost of cialis free cialis levitra side effects generic cialis at walmart female viagra viagra cost cialis testimonials cialis cialis discount viagra generic cialas levitra 10 mg how to take cialis canadian cialis cialis coupon cialis pills for sale viagra activate buy cialis is viagra government funded in america is viagra covered by insurance viagra for sale levitra vs viagra walgreens viagra substitute levitra prices cvs pharmacy viagra coupons generic cialis is viagra government funded cialis generic availability viagra vs cialis vs levitra viagra from canada cialis daily levitra loc us viagra costs side effects of viagra discount cialis cialis 5mg levitra without a doctor prescription what is viagra viagra on line no prec pfizer viagra levitra coupons cialis dosage free viagra levitra cost viagra single packs levitra generic viagra online levitra 10mg generic cialis tadalafil cialis samples cialis price cialis canadian pharmacy cialis pharmacy prices cialis patent expiration levitra cialis medication is viagra federally funded online cialis how much does viagra cost cheap viagra viagra cialis cialis trial cialis for bph cialis effects viagra pills cialis dosage strengths levitra dosage cialis for sale cialis voucher viagra government funding cialis savings card cialis coupon print viagra porn viagra dosage cialis 20mg directions viagra substitute otc viagra levitra online 200 cialis coupon cialis coupon 20 mg cialis online cialis samples overnight cialis for men cialis copay card cialis alternative does viagra work herbal viagra cialis for daily use buy viagra online levitra vs viagra for hardness cialis for women cialis pills canada cialis how does cialis work viagra sex viagra without a doctor prescription usa cialis reviews generic viagra available in usa cialis or viagra wholesale cialis viagra viagra government funded generic viagra 100mg viagra pharmacy generic levitra cialis tadalafil lowest cialis prices cialis vs viagra revatio vs viagra buy levitra natural viagra alternatives that work viagra pills for sale pfizer viagra coupons cialis from canada viagra for women viagra coupon levitra 20 mg cost walmart viagra without prescription when will cialis go generic cialis patent expiration date extended viagra prices viagra single packs cost cialis 5 mg viagra coupons cialis 30 day sample how does viagra work viagra coupons from pfizer daily cialis viagra ingredients cialis dosage recommendations order online viagra viagra savings offer cialis 30 day trial coupon levitra 20 mg what does viagra do is viagra funded by government viamedic cialis viagra girls viagra vs cialis viagra 100mg tablets retail price

Post a Comment

Your email is never published nor shared.
Required fields are marked *