Node.js Sample Program

      No Comments on Node.js Sample Program




Let’s build our first Node Js demo Program.

As Node Js is based on modules and hence for using NodeJs , first you need to include a particular module which you want to use. Then create server on a particular port on which Node Js would be running and then write the logic.

So, basically there are three steps to create a node js program :

1. Include the required module

2. Create Server on a particular port

3. Write the logic code.

var http = require("http");
http.createServer(function(request,response){
   response.writeHead(200, {'Content-Type': 'text/html'});
   response.end("First Demo NodeJs Program");
});

The same above code can also be written as below

var http = require('http');
http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.write('First Demo NodeJs Program');
    response.end();
}).listen(8082);

Now, for running the above code :

1. Save the above program as let’s say demonode.js

2. Traverse to the folder in which it is saved and type on the command prompt node demonode.js

3. Go to browser and hit the below url :

http://localhost:8082/

then the following result would be visible in the browser:
Node Js Sample Program






Leave a Reply