Wednesday, February 15, 2017

Node as web server using HTTP

HTTP

To use HTTP server and client one must use ('http');

The HTTP interface in node are designed to support many features of the protocol which have been traditionally difficult to use.
In particular large chunk-encoded, messages.The interface is careful to never buffer entire requests or responses.



Node HTTP API

  • It is very low level.
  • Deals with stream handling and message parsing only.
  • Parses a message into headers and body but it does not parse the actual headers or the body
  • Listens to the server and respond to them

Cons: We can write our own web server this way hower it would be laborious

Create a simple web server using http

var http =require("http")

//create a server
//One callback that is trying to handle the entire process
//We can write our webserver this way which laborious
var server =http.createServer(function (req,res) 
{
 console.log(req.url);
 //Write some information to he browser
 res.write("<html><body><h1>"+req.url+"</h1></body></html>");
 //This would complete the operation
 res.end()
});
//Listen to the server and create a port number
server.listen(3000);

Run the Server application

c:\Development\NodeProjects\vanilanode>node httpServer
/
/favicon.ico
/api/
/favicon.ico
/api/localhost/
/favicon.ico
/api/testing/
/favicon.ico

Output



No comments:

Post a Comment