我正在尝试使用彗星来实现聊天。由于我没有任何关于彗星的经验,我搜索了一个示例代码,我发现this site
创建所有文件后,我收到了这个模棱两可的错误
未捕获的ReferenceError:未定义的需求(chrome控制台)
ReferenceError:require未定义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>
答案 0 :(得分:0)
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
运行它。