node.js 监听文件变化并打印
1 Use node.js library "chokidar"
2 Detail code:
varwatcher=chokidar.watch("/home/camille/Downloads",{ignored:/[\/\]./,persistent:true});varlog=console.log.bind(console);watcher.on('add',function(path){log('File',path,'hasbeenadded');}).on('addDir',function(path){log('Directory',path,'hasbeenadded');}).on('change',function(path){log('File',path,'hasbeenchanged');}).on('unlink',function(path){log('File',path,'hasbeenremoved');}).on('unlinkDir',function(path){log('Directory',path,'hasbeenremoved');}).on('error',function(error){log('Errorhappened',error);}).on('ready',function(){log('Initialscancomplete.Readyforchanges.');}).on('raw',function(event,path,details){log('Raweventinfo:',event,path,details);})
3 Log:
File /home/camille/Downloads/dd has been added
File /home/camille/Downloads/Untitled Document has been removed
Raw event info: rename dd { watchedPath: '/home/camille/Downloads' }
Raw event info: rename dddd { watchedPath: '/home/camille/Downloads' }
Raw event info: rename dd { watchedPath: '/home/camille/Downloads/dd' }
File /home/camille/Downloads/dddd has been added
File /home/camille/Downloads/dd has been removed
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。