javascript 通过 watchFile 检测 node.js 中的文件更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3806488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect file changes in node.js via watchFile
提问by vito huang
I want to detect changes for a file, if the file changes, I will use child_process to execute a scp command to copy the file to a server.I looked up node.js documentation, the fs.watchFile function seems do what I want to do, but when I tried it, somehow it just doesn't work as I expected. The following code were used:
我想检测文件的更改,如果文件更改,我将使用 child_process 执行 scp 命令将文件复制到服务器。我查找了 node.js 文档,fs.watchFile 函数似乎做我想做的做,但是当我尝试它时,不知何故它并没有像我预期的那样工作。使用了以下代码:
var fs = require('fs');
console.log("Watching .bash_profile");
fs.watchFile('/home/test/.bash_profile', function(curr,prev) {
console.log("current mtime: " +curr.mtime);
console.log("previous mtime: "+prev.mtime);
if (curr.mtime == prev.mtime) {
console.log("mtime equal");
} else {
console.log("mtime not equal");
}
});
With above code, if I access the watched file, the callback function get execute, it will output the same mtime, and always output "mtime not equal" (I only accessing the file). Outputs:
用上面的代码,如果我访问被监视的文件,回调函数得到执行,它会输出相同的mtime,并且总是输出“mtime not equal”(我只访问文件)。输出:
Watching .bash_profile
current mtime: Mon Sep 27 2010 18:41:27 GMT+0100 (BST)
previous mtime: Mon Sep 27 2010 18:41:27 GMT+0100 (BST)
mtime not equal
Anybody know why the if statement failed(also tried using === identify check, but still get the same output) when the two mtime are the same?
有谁知道为什么 if 语句失败(也尝试使用 === 识别检查,但仍然得到相同的输出)当两个 mtime 相同时?
回答by Andris
If mtimeproperties are Dateobjects, then these can never be equal. In JavaScript two separate objects are equal ONLY if they are actually the same object (variables are pointing to the same memory instance)
如果mtime属性是Date对象,那么它们永远不会相等。在 JavaScript 中,两个单独的对象仅在它们实际上是同一个对象时才相等(变量指向同一个内存实例)
obj1 = new Date(2010,09,27);
obj2 = new Date(2010,09,27);
obj3 = obj1; // Objects are passed BY REFERENCE!
obj1 != obj2; // true, different object instances
obj1 == obj3; // true, two variable pointers are set for the same object
obj2 != obj3; // true, different object instances
To check if these two date values are the same, use
要检查这两个日期值是否相同,请使用
curr.mtime.getTime() == prev.mtime.getTime();
(I'm not actually sure if this is the case since I didn't check if watchFileoutputs Date objects or strings but it definitely seems this way from your description)
(我实际上不确定是否是这种情况,因为我没有检查是否watchFile输出 Date 对象或字符串,但从您的描述来看确实是这样)
回答by CEL
For "clever" people:
对于“聪明”的人:
if (curr.mtime - prev.mtime) {
// file changed
}
回答by chbm
Sadly the correct way is
可悲的是,正确的方法是
if (+curr.mtime === +prev.mtime) {}
The + forces the Date object to int which is unixtime.
+ 强制 Date 对象为 int,即 unixtime。
回答by balupton
回答by B T
We use chokidar for file watching, which works even in the dubious context of a centos machine running with a windows file system (vagrant virtualbox centos running on windows machine)
我们使用 chokidar 进行文件监视,即使在运行 Windows 文件系统的 centos 机器(在 Windows 机器上运行的 vagrant virtualbox centos)的可疑上下文中也能正常工作
回答by fullstacklife
quick and nasty solution. If you are not doing a date comparison in terms of previous or after (< or >) and you are simply comparing the datestrings, just do a quick toString() on each one.
快速而讨厌的解决方案。如果您没有根据之前或之后(< 或 >)进行日期比较,而只是比较日期字符串,只需对每个字符串执行快速 toString() 即可。
if (curr.mtime.toString() == prev.mtime.toString())

