極簡 Node.js 入門系列教程:https://www.yuque.com/sunluyong/node
fs.FSWatcher
fs.FSWatcher類 繼承了 EventEmitter,用於監視文件變化,調用 fs.watch 后返回一個 fs.FSWatcher 實例,每當指定監視的文件被修改時,實例會觸發事件調用回調函數
fs.watch('./tmp', (eventType, filename) => {
if (filename) {
console.log(filename);
}
});
fs.watch()
fs.watch(filename[, options][, listener]) 監視文件變化,返回 fs.FSWatcher 實例
- filename:文件或文件夾路徑
- options
- encoding
- recursive:默認值 false,應該監視所有子目錄,還是僅監視當前目錄,僅在 macOS 和 Windows 上支持
- persistent:默認值 true,指示如果文件已正被監視,進程是否應繼續運行
- listener(eventType, filename):文件變化回調函數
eventType 主要是 rename 和 change ,在大多數平台上,文件在目錄中出現或消失時觸發 'rename' 事件,在 Windows 上,如果監視的目錄被移動或重命名,則不會觸發任何事件,當監視的目錄被刪除時,則報告 EPERM 錯誤
fs.watch('./', { recursive: true }, (eventType, filename) => {
console.log(eventType, filename);
});
fs.watchFile()
fs.watchFile(filename[, options], listener) 用於監視文件變化
- filename
- options
- biginit:默認值 false,指定回調 stat 中的數值是否為 biginit 類型
- persistent:默認值 true,當文件正在被監視時,進程是否應該繼續運行
- interval:默認值 5007,用來指定輪詢頻率(ms)
- listener(currentStats, previousStats):listener 有兩個參數,當前的 stat 對象和之前的 stat 對象
要在修改文件時收到通知,則需要比較 curr.mtime 和 prev.mtime
const fs = require('fs');
fs.watchFile('./test.txt', { interval: 100 }, (curr, prev) => {
console.log('當前的最近修改時間是: ' + curr.mtime);
console.log('之前的最近修改時間是: ' + prev.mtime);
});
const tid = setInterval(() => {
fs.appendFile('./test.txt', 'Hello, world!\n', err => {
if (err) throw err;
console.log('文件修改完成');
});
}, 300);
setTimeout(() => {
clearInterval(tid);
fs.unwatchFile('./test.txt');
}, 2000);
fs.watch() 與 fs.watchFile()
因為 fs.watchFile() 使用輪訓方式檢測文件變化,如果不設置 interval 或者設置較高的值會發現文件變化的監視有延遲
而 fs.watch() 監聽操作系統提供的事件,而且可以監視目錄變化,使用 fs.watch() 比 fs.watchFile() 更高效,平常應盡可能使用 fs.watch() 代替 fs.watchFile()
當然 fs.watch() 依賴操作系統的實現,在不同平台上表現會有差異
fs.unwatchFile
fs.unwatchFile(filename[, listener]) 停止監視 filename 的變化,如果指定了 listener,則僅移除此特定監聽器,否則將移除所有監聽器,從而停止監視 filename
fs.unwatchFile('./test.txt');
社區選擇
fs.watchFile() 性能問題,fs.watch() 平台不一致等兩個方法都有不盡如人意的地方
Node.js
fs.watch:
- MacOS 有時候不提供
filename- 在部分場景不觸發修改事件(MacOS Sublime)
- 經常一次修改兩次觸發事件
- 大部分文件變化 eventType 都是
rename.- 未提供簡單的監視文件樹方式
Node.js fs.watchFile:
- 事件處理問題和 fs.watch 一樣爛
- 沒有嵌套監聽
- CPU 消耗大
https://www.npmjs.com/package/chokidar
日常在監視文件變化可以選擇社區的優秀方案
const chokidar = require('chokidar');
// One-liner for current directory
chokidar.watch('.').on('all', (event, path) => {
console.log(event, path);
});
// Initialize watcher.
const watcher = chokidar.watch('file, dir, glob, or array', {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true
});
// Something to use when events are received.
const log = console.log.bind(console);
// Add event listeners.
watcher
.on('add', path => log(`File ${path} has been added`))
.on('change', path => log(`File ${path} has been changed`))
.on('unlink', path => log(`File ${path} has been removed`));
// More possible events.
watcher
.on('addDir', path => log(`Directory ${path} has been added`))
.on('unlinkDir', path => log(`Directory ${path} has been removed`))
.on('error', error => log(`Watcher error: ${error}`))
.on('ready', () => log('Initial scan complete. Ready for changes'))
.on('raw', (event, path, details) => { // internal
log('Raw event info:', event, path, details);
});
