Friday, 26 August 2016

Import HTML Page in Node.js

# Here is the Simple code to import HTML file inside node.js code


/**
 * Created by Anand Dwivedi on 8/26/2016.
 */

var express=require('express');
var path =require('path');
var app =express();


app.get('/',function(request,response){

    response.sendFile('index.html',{root:path.join(__dirname,'')});
});


app.listen(1337,function(){
    console.log('Server Started ');
});

EventEmitter In Node.js

 # It allows programmer to add event listener and to emit the events. Where,

    Event listener : It waits for event to occur such as “start”.
    emitter : It broadcast the event for particular listener.

# EventEmitter provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event.


/**
 * Created by Anand Dwivedi on 8/26/2016.
 */

 var  events =require('events');
var emitter=new events.EventEmitter();

emitter.on('customEvent',function (){
    console.log('custom Event Loaded');
});

emitter.on('customEventParameter',function (name,place){
    console.log(name+' is Staying in '+place);
});

    emitter.emit('customEvent');
    emitter.emit('customEventParameter','Anand','Bangalore');

Read Data from File in Node.js

# Below is the code to Read data from File there are tow ways to read data but better way is to use readFileSync method


/**
 * Created by Anand Dwivedi on 8/25/2016.
 */


var fs=require('fs');

var data=fs.readFileSync('NodeJsSteps.txt');
console.log(data.toString());
console.log('read file Sucessfully');

/**
 var fs=require('fs');

fs.readFile('NodeJsSteps.txt',function (err,data){
   if(err)console.error(err) ;
    console.log(data.toString())
});

 **/

how to write data into File in Node.js

# Below is the code to write data into file

/**
 * Created by Anand Dwivedi on 8/26/2016.
 */


var file = require('fs');

 var data =file.writeFile('File2','Hello my Name is \n Anand','utf8',function (error) {
if(error)console.log('Error while writing into file');
    console.log('sucessfully writeen');

});



and Best Way is 

 /**
 * Created by Anand Dwivedi on 8/26/2016.
 */

 var file = require('fs');
 var data =file.writeFileSync('File2','Hello my Name is \n anand','utf8');
    console.log('sucessfully writeen');

 

#Make sure that you are responsible to create file with the name File2




Thursday, 25 August 2016

Method Call in Node.js

#  Here we will learn how to call Method in Node.js


MethodClient.js 

/**
 * Created by Anand Dwivedi on 8/26/2016.
 */

 var response = require('./method.js');
console.log(response);
/*
Other way to call Method
 */
response.data.addData(2,3);



Method.js


/**
 * Created by Raghavendra on 8/26/2016.
 */

var methods={};
var  output=1000;

methods.eatCookies=function () {
    console.log('eat Method Invoke');
};
methods.SetServer=function () {
    console.log('SetServer Method Invoke');
};

methods.getName=function () {
    console.log('getName Method Invoke');
};

methods.addData=function(a,b){
    output=Number(a)+Number(b);
    console.log('Addition is'+output);
    return output;
}

exports.data=methods;
exports.output=output;





Simple Server Cretation in Node.js


Some Simple Steps to create Server in Node.js . Required Software are

#Any IDE that will support Node.js plugin
# Download All modules from npm  simple command like

 npm install java // responsible to install java module


Programme

/** * Created by Anand Dwivedi on 8/26/2016. */
    var server=require('http');//Download http module

    server.createServer(createServer).listen(1337);// Server Port Number

    function createServer(req, res){
        res.writeHead(200, {'Content-Type': 'text/plain'});

        res.end('Hey there is Some Request on Server'); //Message display to browser
 }




Once programme runs sucessfully open any browser  then type URL :
  http://localhost:1337/
it will print  Hey there is Some Request on Server


Thanks