Thursday, 5 January 2017

connect module

for adding connect modules in the project
on colsole type
npm install connect


use of connect module
1-> first use
/** * Created by deepak gautam on 06-01-2017. */
//here in this code we are showing how we can use 
//2  functions for response 
// one aftre another 
// if comment next() , than 2nd function will be never calledvar connect = require('connect');
var http=require('http');
var app=connect();
function  dofirst(request,response,next)
{

     console.log("this is do first");
     next();

}
function  dosecond(request,response,next)
{

    console.log("this is do second");
    next();

}
app.use(dofirst);
app.use(dosecond);

http.createServer(app).listen(8888);


2-> second use

//if   we need to call different functions accordig to url than also we can use 
// connect.

/** * Created by deepak gautam on 06-01-2017. */
var connect = require('connect');
var http=require('http');
var app=connect();


function profile_page (request,respose) {
     console.log("this is profile page ");

}
function details_page (request,respose) {
    console.log("this is details page ");

}
app.use('/profile',profile_page);
app.use('/details',details_page);
http.createServer(app).listen(8888);

http request and response

normal string reques and response
/** * Created by deepak gautam on 05-01-2017. */
var http=require('http');
// function have 2 parameters  one is response and second is request// reuest parameters have details about the request made// like requesting url// response is the parameter in which we have to bind result of the requestfunction onRequest(request,response)
{ console.log("server function called "+request.url);
  response.writeHead(200,{"Context-Type":"text/plain"});
  response.write("hello this is my first code for  for ser");
  response.end();
}


console.log(" this is a server side script");
http.createServer(onRequest).listen(8888);
//setInterval(onRequest,1000);



// html page response


/** * Created by deepak gautam on 05-01-2017. */
var http=require('http');
var fs=require('fs');

function error_page(response)
{     console.log("there is some error with the page ");
    response.writeHead(404,{"Context-Type":"text/plain"});
    response.write(" error 4040:: page not found");
    response.end();
}

// function have 2 parameters  one is response and second is request// reuest parameters have details about the request made// like requesting url// response is the parameter in which we have to bind result of the request
function onRequest(request,response)
{  console.log("server function called "+request.url);
 if(request.method="GET" && request.url=='/' )
      {
          response.writeHead(200,{"Context-Type":"text/html"});
          fs.createReadStream("./index.html").pipe(response);
      }
      else      {
           error_page(response);
      }

}


console.log(" this is a server side script");
http.createServer(onRequest).listen(8888);
//setInterval(onRequest,1000);

file module and path module

/** * Created by deepak gautam on 05-01-2017. */
// reading and  eriting  filevar fs=require('fs');
fs.writeFileSync("abc.txt","hello coderes lets build something new ");
console.log(fs.readFileSync("abc.txt").toString());


// path module// path modules are mainly used for dealing with the path of any link// we can alot of things wit path
var path=require('path');
var homepage="deepkagautam/desktop//mywebsite/index.html";
console.log(path.normalize(homepage));//deepkagautam\desktop\mywebsite\index.htmlconsole.log(path.dirname(homepage));//deepkagautam/desktop//mywebsiteconsole.log(path.extname(homepage));//.htmlconsole.log(path.basename(homepage));//index.html

// inbuilt functions
console.log(__filename);//C:\Users\deepak gautam\WebstormProjects\fnodejs\file_operations.jsconsole.log(__dirname);//C:\Users\deepak gautam\WebstormProjects\fnodejs

exporting functions from one module to another modul


suppose  we want to export  a function from one .js file to another js file .


/** * Created by deepak gautam on 05-01-2017. */

suppose this is a code in movies.js 
here we are exporting 2 functions 
1->sar 
2->sal 
module.exports= {
    sar :function   ()
    {
        console.log("best movies of sarukh is  chennai express, fans, rawan");

    },

    sal : function  () {
        console.log("best movies of salman  is  munna bhai mbbs  , bajranga bhaijan, ....");

    }
};



// this is the code in app.js file where we are importing the exported  functions in movies.js
var user=require('./movies');// since both modules are  in the same folder so we are using this path
user.sar();



problems with exported  variables ..

suppose we export one variable say x=10,
now two mudules import this variable, 
than if one module change that variable than other module will get the updated value ..


for fixing that we have to do it in a different way
always return a new  created variable