node.js v0.82的fs.watch只能监听第一级目录文件的创建修改删除, 对于更深层次的目录文件的创建删除笼统地识别为"创建", 并且自始至终不能定位到哪个目录文件被删除!
测试代码:
var fs = require('fs'), path = require('path');
console.log(__dirname);
fs.watch(__dirname, function (event, filename) {
if(filename){
var type = event == "change" ? "changed" : "created"; //有文件或目录发生改变或被添加
var filepath = path.join(__dirname ,filename);
var stat = fs.statSync(filepath);
"isDirectory,isFile,isSymbolicLink".replace(/\w+/g,function(method){
if(stat[method]()){
console.log( method.replace(/^is/,"")+ " '"+ filepath +"' has "+ type)
}
});
}else{
console.log("Some file is removed") //删除了某个文件或目录
}
});