Thursday, 5 January 2017

http request and response

normal string reques and response
/** * Created by deepak gautam on 05-01-2017. */
var http=require('http');
// function have 2 parameters  one is response and second is request// reuest parameters have details about the request made// like requesting url// response is the parameter in which we have to bind result of the requestfunction onRequest(request,response)
{ console.log("server function called "+request.url);
  response.writeHead(200,{"Context-Type":"text/plain"});
  response.write("hello this is my first code for  for ser");
  response.end();
}


console.log(" this is a server side script");
http.createServer(onRequest).listen(8888);
//setInterval(onRequest,1000);



// html page response


/** * Created by deepak gautam on 05-01-2017. */
var http=require('http');
var fs=require('fs');

function error_page(response)
{     console.log("there is some error with the page ");
    response.writeHead(404,{"Context-Type":"text/plain"});
    response.write(" error 4040:: page not found");
    response.end();
}

// function have 2 parameters  one is response and second is request// reuest parameters have details about the request made// like requesting url// response is the parameter in which we have to bind result of the request
function onRequest(request,response)
{  console.log("server function called "+request.url);
 if(request.method="GET" && request.url=='/' )
      {
          response.writeHead(200,{"Context-Type":"text/html"});
          fs.createReadStream("./index.html").pipe(response);
      }
      else      {
           error_page(response);
      }

}


console.log(" this is a server side script");
http.createServer(onRequest).listen(8888);
//setInterval(onRequest,1000);

No comments:

Post a Comment