【问题标题】:Uncaught ReferenceError by JS for implementing Comet用于实现 Comet 的 JS 未捕获的 ReferenceError
【发布时间】:2013-02-06 08:05:58
【问题描述】:

我正在尝试使用彗星来实现聊天。由于我没有任何彗星经验,因此我搜索了示例代码,并找到了this site

创建所有文件后,我得到了这个模棱两可的错误

未捕获的 ReferenceError:未定义要求(chrome 控制台)
ReferenceError:未定义要求 var http = require('http');(firefox 控制台)

这是因为我在 localhost 服务器上测试此代码还是有其他问题? html代码是:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Comet Test</title>
    </head>
    <body>
        <p><a class='customAlert' href="#">publish customAlert</a></p>
        <p><a class='customAlert2' href="#">publish customAlert2</a></p>
        <script src="http://localhost:80/chekhabara/downloads/realtime/2/jquery.min.js" type="text/javascript"></script>
        <script src="http://localhost:80/chekhabara/downloads/realtime/2/NovComet.js" type="text/javascript"></script>
        <script type="text/javascript">

            var http = require('http');

            http.createServer(function (req, res) {
              res.writeHead(200, {'Content-Type': 'text/plain'});
              res.end('Hello World\n');
            }).listen(80, '127.0.0.1');
            console.log('Server running at http://127.0.0.1:80/');
        </script>
    </body>
</html>

【问题讨论】:

  • 这是node.js 代码。你不要把它放在一个 HTML 文件中——你必须运行一个特殊的 node.js 服务器。
  • 您的代码似乎与您链接的内容无关。
  • 他正在尝试运行节点服务器,那是他的 sn-p。
  • @AlienWebguy:不,那是一个 HTML 文件。
  • 不,这不是 node.js 我知道什么是 node.js,我不想使用那种技术。它是 jquery 库 NovComet.js

标签: php javascript comet


【解决方案1】:

require 未定义,因为它不是window 的属性。

安装NodeJS,把这段代码放到一个新文件里,比如myscript.js

        var http = require('http');

        http.createServer(function (req, res) {
          res.writeHead(200, {'Content-Type': 'text/plain'});
          res.end('Hello World\n');
        }).listen(80, '127.0.0.1');
        console.log('Server running at http://127.0.0.1:80/');

并从命令行% node myscript.js 运行它。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 2020-06-21
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 2023-01-23
  • 2020-08-24
相关资源
最近更新 更多