【发布时间】:2012-11-21 14:45:42
【问题描述】:
我正在尝试编写一个 node.js 脚本来监视文件目录中的更改,然后打印更改的文件。如何修改此脚本以使其监视目录(而不是单个文件),并在更改目录中的文件时打印它们的名称?
var fs = require('fs'),
sys = require('sys');
var file = '/home/anderson/Desktop/fractal.png'; //this watches a file, but I want to watch a directory instead
fs.watchFile(file, function(curr, prev) {
alert("File was modified."); //is there some way to print the names of the files in the directory as they are modified?
});
【问题讨论】:
-
我想知道这是否相关:stackoverflow.com/questions/12063266/…(不过我不熟悉 gruntjs - 我希望有其他解决方案。)
-
也许像 node-inotify-plusplus 这样的东西会有用:stackoverflow.com/questions/5877263/…
-
你运行的是什么操作系统?这很重要,因为 Unix/Linux 使用
inotify,OSX 使用FSWatch和我不知道 Windoze,但我相信 Google 可以告诉你,查看文件的低级机制有很大不同。 -
虽然我最初在我的项目中使用了普通的
node-inotify,但我最终切换到node-inotify-plusplus,因为谁不喜欢抽象? =)
标签: node.js