美文网首页
nodejs中监听文件更改

nodejs中监听文件更改

作者: Armin0202 | 来源:发表于2017-01-20 16:09 被阅读526次
    • chokidar是一个很好用的nodejs下监听文件更改的插件,配置也超级简单。传送门
      var path = require('path')
      var Chokidar = require('chokidar');
      var watcher = Chokidar.watch([path.join(__dirname, '../src')], {
        // ignored: /(^|[\/\\])\../, 
        persistent: true,
        usePolling: true,
      });
      var log = console.dir.bind(console);
      var watchAction = function({event, eventPath}){
        log(`Has been ${event}ed, file: ${eventPath}`);
        // 这里进行文件更改后的操作
      }
      watcher
      .on('ready', () => log(`Initial scan complete. Ready for changes.`))
      .on('add', path => log(`File ${path} has been added`))
      .on('change', path => watchAction({event: 'change', eventPath: path}))
      .on('unlink', path => watchAction({event: 'remove', eventPath: path}));
    
    

    相关文章

      网友评论

          本文标题:nodejs中监听文件更改

          本文链接:https://www.haomeiwen.com/subject/mnawbttx.html