Tuesday 6 June 2017

How to watch two different files with chokidar on server?

I want to watch two files with chokidar in NodeJS the following way:

  • set up a watcher for file1, periodically download a file under this name and when the download is finished, edit the content of file2
  • set up a watcher for file2 and when its content is modified, change the exports.foo variable's value

This concept works very well on localhost (under Windows), but when I deploy the code to the server the change of file2's content is not noticed. Simply the change event is not fired. Below is the code (note that the file1 is downloaded every 5 minutes from somewhere and the change of this file is noticed):

chokidar.watch(file2, {
        awaitWriteFinish: {
            stabilityThreshold: 2000,
            pollInterval: 100
        }
    }
).on('all', function (event, path) {
    if (fs.existsSync(file2)) {
        fs.readFile(file2, function (err, data) {
            if (!err) {
                file2_content = readFileContent(file2);
                exports.foo= file2_content;
            }
        });
    }
});

chokidar.watch(file1, {
        awaitWriteFinish: {
            stabilityThreshold: 2000,
            pollInterval: 100
        }
    }
).on('all', function (event, path) {
    if (fs.existsSync(file1)) {
        fs.readFile(file1, function (err, data) {
            if (!err) {
                /* 
                   modify file2's content based on data in file1
                   (for this purpose fs.writeFile() function is used)
                */
            }
        });
    }
});

What could be the problem with this implementation of my idea? Why does this work on localhost, but fails on the server?



via Thookes

No comments:

Post a Comment