我想检测文件的更改,如果文件更改,我将使用child_process执行scp命令以将该文件复制到server.I查找node.js文档,fs.watchFile函数似乎做我想做的事情,但是当我尝试它时,不知何故,它不能像我所期望的那样工作。下面的代码被用于:通过watchFile检测node.js中的文件更改
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");
}
});
有了上面的代码,如果我进入被监视的文件,回调函数得到执行,它将输出相同的mtime,始终输出“的mtime不等于”(我只访问文件)。输出:
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
任何人都知道为什么,如果语句失败(也使用===识别检查试过,但仍然得到同样的输出)当两个的mtime是一样的吗?
谢谢,我没有意识到,mtime属性是一个对象(我认为他们是字符串对象:(我使用typeof curr.mtime,它返回对象,有没有一种方法在JavaScript中找出什么样的对象在你给出的例子中,我认为obj1 == obj2,因为你只是改变了相等性检查,只有当他们做了识别检查obj1 ==== obj2。 – 2010-09-27 22:03:19
你可以使用op erator'instanceof'就像这样:'var d = new Date(); if(d instanceof Date == true)alert(1);'或者取得文本对象类型使用'var d = new Date(); Object.prototype.toString。调用(d); //“[object Date]”' – Andris 2010-09-28 08:45:30
我注意到的一件事是,如果我做了这样的事情:__var i = 10; if(i instanceof Number)alert(1); __ if语句从不为真(typeof i =>“number”and Object.prototype.toString.call(i)=>“[object Number]”,但是如果我声明i作为:var i =数字(10),它的工作原理。任何人都知道背后的原因? – 2010-09-29 05:11:11