Tuesday 29 March 2016

First node.js application

Go to URL : node.js download the software.

On window click on node.js exe to start.

output : >

Basic operation :

> var x
undefined
. define the variable and initialise the value

x= 1000
> x
output : 1000

and var y, and initialise y=09 and print variable y.

Now perform the basic operation.

> x+y
output :  1009

Now creating the application

step 1: 

create the server.js file.

a. define the module "http" for making the request
ex : var http = require("http");
b. call the server instance using http and will listen to port(8888), bind the port the server
ex:
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);
c. In the above function pass the request and response parameter.
    function(request, response) {
     ...
    }
d. On making a request. it will call the request
ex : http://localhost:8888/
e. For Response bind the content-type and status code and content to display
ex : response.writeHead(200, {"Content-Type": "text/plain"});
       response.write("Hello World");

step 2 :

a. go to command prompt and go the node.exe file and open it
ex : node server.js
output : Hello World

step 3:

Go to browser and make request to nodejs server
a. http://localhost:8888/
b. output :
Hello world






No comments:

Post a Comment