2016-12-26 42 views
0

我有下面的代码段如下所示:捕获返回的函数以JavaScript

function(staticPath) { 
    console.log('static Path = ' +staticPath); 
    return function(data, response) { 

     console.log('data = ' +data); 
     console.log('response = ' +response); 
     var readStream; 
     // Fix so routes to /home and /home.html both work. 
     data = data.replace(/^(\/home)(.html)?$/i, '$1.html'); 
     data = '.' + staticPath + data; 
     fs.stat(data, function(error, stats) { 
      if (error || stats.isDirectory()) { 
       return exports.send404(response); 
      } 
      readStream = fs.createReadStream(data); 
      return readStream.pipe(response); 
     }); 
    } 
} 

该函数基本上解析路径到一个HTML文件,并显示该文件的内容。

我无法理解如何从外部调用此方法。

我打电话它如下:

staticFile("D:\\Node Applications\\public\\home.html") 

我可以捕获它的可变innerFunc内,我可以调用内部功能innerFunc(response)其中响应HTTP的serverResponse其中我有参考与我,但我不知道我怎么能通过data帕拉姆。

我不理解幕后发生了什么。谁能解释一下?我们经常在javascript中遇到这种类型的代码吗?

编辑: 为了把事情说清楚: 还有另一种方法如下:

function(data, response) { 
    response.writeHead(200, { 
     'Content-Type': 'application/json' 
    }); 
    response.end(JSON.stringify(data)); 
} 

这是我从我的节点服务器的逻辑调用如下:

http.createServer(function(req, res) { 
    // A parsed url to work with in case there are parameters 
    var _url; 
    // In case the client uses lower case for methods. 
    req.method = req.method.toUpperCase(); 
    console.log(req.method + ' ' + req.url); 

    if (req.method !== 'GET') { 
     res.writeHead(501, { 
      'Content-Type': 'text/plain' 
     }); 
     return res.end(req.method + ' is not implemented by this server.'); 
    } 
    if (_url is something like //localhost:1337/employees) { 
      //call employee service which returns *data*. 
      // send the data with a 200 status code 
      return responder.sendJson(data, res); 
     }); 
    } else { 
     // try to send the static file 
     /*res.writeHead(200); 
     res.end('static file maybe');*/ 
     console.log('Inside else'); 
     var staticInner = responder.staticFile("D:\\Node Applications\\public\\home.html"); 
     staticInner(res); 
    } 

    // res.end('The current time is ' + Date.now()) 
}).listen(1337, '127.0.0.1'); 

正如人们所看到有没有数据变量传递给innerFunc因此,感到困惑。

+0

你明白这个功能应该做什么吗?如果你不知道如何传递数据(我认为你的意思是你不知道'data'是什么,因为你可以将它作为'innerFunc(data,response)')来传递,我会说你不会不知道这个功能是干什么的。很高兴知道这一点。此外,你是否期望得到这样的结果:'返回readStream.pipe(响应)',或者什么? – Parziphal

+0

你说得对。我编辑了我的问题,因为您可以看到我没有'data'变量将它传递给函数。最初,我以为我可能会错过一些理解,但似乎数据变量是我需要将它传递给函数。 你能解释什么可能是这里从函数返回函数的原因?对于所有这些都需要什么? 谢谢! – Adithya

回答

1

根据“innerFunc”,好像data是一些字符串,与staticPath连接,形成一个文件路径。我们可以看到,该路径使用fs.stat进行测试。也许客户端应该发送一些自定义数据?无论如何,data似乎是一个非常糟糕的名字,像这样的变量。

这似乎是该函数试图做的是发送一个文件作为响应?什么staticPath应该是相对于JavaScript文件,其中的代码是一个路径,因为它是串联这样的:

data = '.' + staticPath + data; 

这将在类似./some/path/index.html结束。 staticPath的值将是/some/path,data的值将是index.html。因此,如果您的JS文件位于/home/foo/node/index.js中,则“innerFunc”将尝试查找的文件将位于/home/foo/node/some/path/index.html中。

如果您将"D:\\Node Applications\\public\\home.html"转换为staticFile(),您会得到类似于.D:\\Node Applications\\public\\home.htmlindex.html的文件,这显然是无效文件。

要回答什么是函数的返回功能

在这种情况下来看,作为mgouault说,这是没有意义的做到这一点。也许程序员有一些想法,但在编程时改变它,函数结束了(有时会发生)。

0

您的功能(例如:function(staticPath))应该有一个名称或存储在变量中以便能够调用它们。所以我猜测你将所有这些代码存储在变量staticFile中。

然后当你用参数调用它时:staticFile("D:\\Node Applications\\public\\home.html")它返回一个函数(return function(data, response) {...})。

现在,你有这种内在的功能,您可以用dataresponse称之为:

var innerFunc = staticFile("D:\\Node Applications\\public\\home.html"); 
innerFunc(data, response); 

这将响应使用可变response(我猜)。

返回函数的函数在javascript中很常见,尤其是在为特定用途使用闭包时,但在这种情况下,它并不真正有用。