I want to watch a specific file to look for specific changes using Node and also fetch those changes in the file. Is there anyway doing that using fs?
Asked
Active
Viewed 2,228 times
1
-
possible duplicate of [Observe file changes with node.js](http://stackoverflow.com/questions/13698043/observe-file-changes-with-node-js) – James Jul 23 '15 at 09:32
1 Answers
1
Yes, you can do it by using fs.
How to do it:
- Load file content
- Watch file
- Compare content with the previous one when
change
event is triggered - Update file content
It should look like this:
fs.watch('foo', function (event, filename) {
if (event == 'change') {
// Load file content
// Compare to one previously loaded
// actualContent = newContent
}
});
See:
- fs.watch documentation: https://nodejs.org/api/fs.html#fs_fs_watchfile_filename_options_listener
- Event change documentation: https://nodejs.org/docs/latest/api/fs.html#fs_event_change

Erazihel
- 7,295
- 6
- 30
- 53