Search This Blog

2015-10-23

Creating an HTTP Server using Node JS

One of the purposes Node.js is commonly used for is creating HTTP servers capable of responding to client requests.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: Create Http Server using 'createServer' method of 'http' module

var http=require('http');

var handlerMethod=function(req,res){
 res.end("Hello, This is a message from the handler of web server");
}

http.createServer(handlerMethod).listen(1234,'localhost');

console.log("Http Server is running");

Step 4: Run it using 'node index.js'

Step 5: open your brower and browse 'http://localhost:1234' to see the output.

It will display the message 'Hello, This is a message from the handler of web server' ,as mentioned in your response object.

No comments: