Tuesday 9 May 2017

Monitoring Multiple Directories For Adding / Removing Files in Node.js

I need to monitor if some files are created/removed from certain directories.

I've tried to use fs.watch:

let dirs = ['/tmp', '/home/user', './some-dir']

dirs.forEach(function(dir) {
   fs.watch(dir, (evt, filename) => {
      if (filename && evt === "rename") {
        console.log(evt, dir, filename)
      }
   })
})

But above code will keep returning latest directory in the array. Which is logical. In the same time, the file was added/removed from another directory.

There are some nice third-party modules around such as https://www.npmjs.com/package/chokidar But it has lots of dependencies, so I'm trying to keep up with native node solution for now.



via Systems Rebooter

No comments:

Post a Comment