【发布时间】:2014-07-15 11:24:57
【问题描述】:
我使用 chokidar 来监控文件夹中的文件是否已更改。目前,当用户在我的 ExperimentController 中更新 Experiment Model 时会触发它。
var chokidar = require('chokidar');
...
var watcher = chokidar.watch('assets/output-model-files', {ignored: /[\/\\]\./, persistent: true});
watcher.on('change', function(path) {
...read my changed file and update the content of my database, and send a socket publishUpdate message...
... read file content in an asynchronous way
fs.readFile(path,"utf-8", function (err, data) {
... update the experiment object with the content of the changed file
Experiment.update(req.param('id'), ExpObj, function expUpdated(err) {});
... send a message via socket saying that the exeriment object has been updated
Experiment.publishUpdate(req.param('id'), {name: exp.name,
results: JSON.stringify(myres),
action: ('file has just been updated. nb of trajectories: '+totalNbTrajectories)
});
但我想持续监控目标文件夹中的任何更改,并在sails.js 服务器启动时发送 Experiment.publishUpdate 消息,而不是在用户更新实验对象时发送。 我可以在哪里将该 chokidar.watch(...) 代码放在服务器端,以便从文件更改中更新实验对象? socket.js?
【问题讨论】: