我需要在WAMP Server中查看一个文件,以便当文件更改时,javascript可以运行一个函数。
我已经做了一些搜索,并看到了库Node.js,但我有一个问题,使用在wamp服务器。我运行了Node.js安装程序,以便安装在我的计算机中。但当我在我的网页上这么说时:
var fs = require('fs');
console.log('Watching watcher.txt');
fs.watchFile('watcher.txt', 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');
}
});我得到了一个错误:“未定义的ReferenceError: require”。
有人能帮我吗?Wamp中的Node.js在我的脑海中做了很多混乱的事情。
发布于 2016-09-12 10:46:24
我已经找到了解决办法。使用这个简单的代码,并且不需要在WAMP Server中安装任何库。
解决方案: Is it possible to retrieve the last modified date of a file using Javascript?
发布于 2016-09-12 10:32:24
如果您的意思是要在服务器上运行它,您需要在同一台服务器上分别安装Node.js和WAMP,同时运行它们,然后将Node.js脚本指向WAMP目录中的一个文件。
Node.Js和PHP都是为网站编写后端程序的不同方式,所以这通常意味着一个网站要么运行在节点上,要么运行在PHP上,而不是同时运行。我认为在您的情况下(假设您使用PHP构建了您的网站),您最好在PHP中创建一个文件处理程序。
看看https://github.com/PHPGangsta/FileWatcher,获得一些关于如何做到这一点的灵感。
https://stackoverflow.com/questions/39447988
复制相似问题