5
5

More than 5 years have passed since last update.

Nodejsで自前のwatchを作る

Posted at

ファイルの監視

sample1.js
var fs = require('fs'); // ファイル操作


// ウォッチするファイルの指定と保存時に実行される処理の記述
createWatcher('path/to/hoge.txt', function() {
    console.log("Callback");
});

// ウォッチのテンプレ。100msなので、監視対象が増えると辛いかもしれない。
function createWatcher(aTargetPath, aCallback) {

    fs.watchFile(aTargetPath, { persistent: true, interval: 100 }, function(curr, prev) {

        console.log("\n// ----------------- Saved ");

        // 保存時にやりたい処理
        aCallback();
    });
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5