用node.js apirecursion地观察一个目录

我试图recursion观看一个目录,我磕磕绊绊的命名空间问题。

我的代码如下所示:

for (i in files) { var file = path.join(process.cwd(), files[i]); fs.lstat(file, function(err, stats) { if (err) { throw err; } else { if (stats.isDirectory()) { // Watch the directory and traverse the child file. fs.watch(file); recursiveWatch(file); } } }); } 

看来我只看最后一个目录stat'd。 我相信问题是循环在lstatcallback完成之前完成。 所以每次调用lstatcallback,file =。 我如何解决这个问题? 谢谢!

你可以考虑使用:(假设ES5和files是一个文件名Array

 files.forEach(function(file) { file = path.join(process.cwd(), file); fs.lstat(file, function(err, stats) { if (err) { throw err; } else { if (stats.isDirectory()) { // Watch the directory and traverse the child file. fs.watch(file); recursiveWatch(file); } } }); }); 

有这个节点的手表包。