【问题标题】:Using Node.js to watch file in WAMP Server使用 Node.js 在 WAMP 服务器中监视文件
【发布时间】:2017-01-19 18:05:47
【问题描述】:

我需要在 WAMP 服务器中监视一个文件,以便当文件更改时 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');
                }   
            });

我得到这个错误:“Uncaught ReferenceError: require is not defined”。

有人可以帮助我吗? Wamp Server 中的 Node.js 让我很困惑。

【问题讨论】:

    标签: javascript php node.js wamp wampserver


    【解决方案1】:

    我找到了解决方案。使用这个简单的代码,无需在 WAMP Server 中安装任何库。

    解决方案: Is it possible to retrieve the last modified date of a file using Javascript?

    【讨论】:

      【解决方案2】:

      如果您的意思是要在服务器上运行它,则需要在同一服务器上将 Node.js 与 WAMP 分开安装,同时运行它们,然后将 Node.js 脚本指向 WAMP 目录中的文件。

      Node.Js 和 PHP 都是为网站编写后端程序的不同方式,因此这通常意味着网站可以在 Node 或 PHP 上运行,但不能同时在两者上运行。 我认为在您的情况下(假设您使用 PHP 构建网站)您最好在 PHP 中创建文件观察器。

      查看https://github.com/PHPGangsta/FileWatcher 以获得有关如何执行此操作的一些灵感。

      【讨论】:

      • 愿意分享你的所作所为吗?
      • 是的,看看我的新帖子
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      相关资源
      最近更新 更多