fs.FSWatcher is an EventEmitter that is used to watch the changes/updates in a directory. fs.watch() will emit a 'change' event when a file is modified. We will get a better understanding by looking into it's implementation.
Here we have assigned a directory that needs to be watched, which means if there are any modifications to be made in this directory the fs.watch() (i.e watcher in our case) function will emit a change event.
OR
Hence , FSWatcher is a simple event emitter with convenient stat()/close() methods. Let's take a deeper look now into the close method
In case you want to stop watching the current being watched file/directory you can call the close() method which will stop the watcher looking for changes.
You can also take a look at Node.JS offical document on FSWatcher
var fs = require('fs'); var watcher = fs.watch(__dirname);
Here we have assigned a directory that needs to be watched, which means if there are any modifications to be made in this directory the fs.watch() (i.e watcher in our case) function will emit a change event.
watcher.on('change', function name(event, filename){ // your code here });
OR
fs.watch('./tmp', { encoding: 'buffer'}, (eventType, filename) => { if(filename){ // your code here } });
In the example above scenario we are providing the filename as a buffer other wise filename will be a UTF-8 string.Hence , FSWatcher is a simple event emitter with convenient stat()/close() methods. Let's take a deeper look now into the close method
Close():
watcher.close()
In case you want to stop watching the current being watched file/directory you can call the close() method which will stop the watcher looking for changes.
Error event:
In case an error orrcurs while watching the file error event is emitted.You can also take a look at Node.JS offical document on FSWatcher
Comments
Post a Comment