Skip to content

Instantly share code, notes, and snippets.

@fnobi
Created January 2, 2013 17:33
Show Gist options
  • Save fnobi/4436348 to your computer and use it in GitHub Desktop.
Save fnobi/4436348 to your computer and use it in GitHub Desktop.
fs.watchFileを使ってみた ref: http://qiita.com/items/b4588b2b37fe7136383d
var fs = require('fs');
var filepath = 'message.txt';
// filepathが存在しなくても、watchFileはエラーとか出してくれないので、
// 先にfs.existsで調べておく
if (!fs.existsSync(filepath)) {
console.error('%s doesn\'t exist.', filepath);
process.exit();
}
console.log('start watch: %s', filepath);
fs.watchFile(filepath, function (curr, prev) {
// プロパティ丸ごと出力してもらう
console.dir(curr);
// 以前のmtimeと比較
console.log('the current mtime is: ' + curr.mtime);
console.log('the previous mtime was: ' + prev.mtime);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment