共享响应对象nodejs

时间:2015-01-28 11:44:42

标签: node.js callback

我有以下代码在节点中发送page.html或任何其他html文件。我正在使用大量的嵌套(示例1),如果我不使用它并使用命名函数(示例2),我要声明变量来保持响应,是否有更好但更简单的替代方法,如bind或任何其他策略?

// 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();
    }


}

这是样本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();
  }
}

2 个答案:

答案 0 :(得分:1)

将局部变量传递给命名函数的一种简单方法是将其包装在匿名函数(或与变量在同一范围内的本地函数)中。例如:

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

正如我在评论中所提到的,在Node.js文档中不鼓励使用fs.exists()

答案 1 :(得分:0)

我实施了fd的建议,并提出了以下解决方案。欢迎改进:)

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();
  }
}