Bharat
Bharat

Reputation: 152

Share response object nodejs

I've the following code that sends an page.html or any other html file in node. I'm using a lot of nesting(sample 1), if i don't use it and use named functions(sample 2), i've to declare variables to hold response, is there a better but simple alternative like bind or any other strategy ?

// Sample 1
var http = require('http'); 
var fs = require('fs');

var server = http.createServer(); 
server.on("request",requestHandler);

server.listen(8080);

function requestHandler(request,response){

    var regPageName = /(\w+\.\w+)+$/;

    var urlRequested = request.url;
    var match = regPageName.exec(urlRequested);

    if(match!=null){
        var fileName = match[1]; // first capture group is filename

        // check for existence of file
        fs.exists(fileName,function(doesExist){
            if(doesExist){
                // file exists dump it now
        response.writeHead(200,{"Content-Type": "text/html"});
        fs.readFile(fileName,function(err,data){
          if(err){
            throw err;
          }
          else{
            response.write(data.toString());
            response.end();
          }
        }); // readfile
            }// exists
            else{
                response.writeHead(404,'Not Found');
                response.end();
            }
        })
    }
    else {
        response.writeHead(400,'Bad Request');
        response.end();
    }


}

And here is sample 2

// Sample 2
var http = require('http'); 
var fs = require('fs');
var glrequest;
var glresponse;
var glfilename ;
var server = http.createServer(); 
server.on("request",requestHandler);

server.listen(8080);

function requestHandler(request,response){
  glrequest = request;
  glresponse = response;
    var regPageName = /(\w+\.\w+)+$/;

    var urlRequested = request.url;
    var match = regPageName.exec(urlRequested);

    if(match!=null){
        var fileName = match[1]; // first capture group is filename
        glfilename = fileName;
        // check for existence of file
        fs.exists(fileName,CheckExistence);
    }
    else {
        response.writeHead(400,'Bad Request');
    response.end();
    }
}// requestHandler

function CheckExistence(doesExist){
  if(doesExist){
    // file exists dump it now
    glresponse.writeHead(200,{"Content-Type": "text/html"});

    fs.readFile(glfilename,dumpData); // readfile
  }// exists
  else{
    glresponse.writeHead(404,'Not Found');
    glresponse.end();
  }
}

function dumpData(err,data){
  if(err){
    glresponse.end();
    throw err;
  }
  else{
    glresponse.write(data.toString());
    glresponse.end();
  }
}

Upvotes: 0

Views: 106

Answers (2)

Bharat
Bharat

Reputation: 152

I implemented suggestions by fd and came up with the following resolution. Improvements are welcome :)

var http = require('http'); 
var fs = require('fs');

var server = http.createServer(); 
server.on("request",requestHandler);

server.listen(8080);
//1
function requestHandler(request,response){
    var regPageName = /(\w+\.\w+)+$/;

    var urlRequested = request.url;
    var match = regPageName.exec(urlRequested);

    if(match!=null){
        var fileName = match[1]; // first capture group is filename
        // check for existence of file
        fs.exists(fileName,function(doesExist){
      CheckExistence(doesExist,fileName,response);}
    );
    }
    else {
        response.writeHead(400,'Bad Request');
    response.end();
    }
}// requestHandler
//1.1
function CheckExistence(doesExist,fileName,response){
  if(doesExist){
    // file exists dump it now
    response.writeHead(200,{"Content-Type": "text/html"});
    fs.readFile(fileName,
      function(err,data){
      dumpData(err,data,response);}
    ); // readfile
  }// exists
  else{
    response.writeHead(404,'Not Found');
    response.end();
  }
}
//1.1.1
function dumpData(err,data,response){
  if(err){
    response.end();
    throw err;
  }
  else{
    response.write(data.toString());
    response.end();
  }
}

Upvotes: 0

Mike Tunnicliffe
Mike Tunnicliffe

Reputation: 10772

One simple way to pass the local variable to the named function is to wrap it in an anonymous function (or a local function in the same scope as the variable). Eg:

        fs.exists(fileName,function(exists) { CheckExistence(exists, filename); );
    ...
    function CheckExistence(doesExist, filename){
        if(doesExist){
        ...

As mentioned in my comment, use of fs.exists() is discouraged in the Node.js doc.

Upvotes: 1

Related Questions