我无法获取now.js 聊天客户端教程 http://nowjs.com/examples/hw上班。 (我也关注过这个视频 http://vimeo.com/20936525#at=0几乎一模一样)。
服务器.咖啡:
fs = require 'fs'
http = require 'http'
now = require 'now'
server = http.createServer (req, res) ->
fs.readFile(
'index.html'
(err, data) ->
res.writeHead(
200
'Content-Type': 'text/html'
)
res.end(data)
)
server.listen 8080
everyone = now.initialize(server)
everyone.now.distributeMessage = (msg) ->
everyone.now.receiveMessage(@.now.name, msg)
索引.html:
<html>
<head>
<title>nowjs title</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/Flotype/now/master/lib/now.js"></script>
<script type="text/javascript">
$(document).ready(function() {
now.name = prompt("What's your name?", "");
now.receiveMessage = function(name, msg) {
return $("<div></div>").text("" + name + ": " + msg).appendTo("#msg");
};
return $("#send-button").click(function() {
now.distributeMessage($("#text-input").val());
return $("#text-input").val("");
});
});
</script>
</head>
<body>
<div id="msg"></div>
<input type="text" id="text-input">
<input type="button" value="Send" id="send-button">
</body>
</html>
当我加载服务器时node server.js
,
我在 now.js 的第 1 行收到一条错误消息“require not Define”。因此,客户端代码无法找到变量“now”。
我知道“require”是一个节点函数,但是如何让客户端理解这一点?
任何帮助将不胜感激。
您包含在客户端源中的文件(../Flotype/now/master/lib/now.js
) 是调用时包含在节点进程中的 Node 服务器端代码now = require 'now'
.
因此,将您包含的客户端源文件更改为.../Flotype/now/master/lib/now.js
to /nowjs/now.js
会解决你的问题。
/nowjs/now.js 文件来自哪里?
使用 NowJS(以及许多其他进行客户端/服务器通信的 npm 包)时,您可以扩展服务器对象。这是用线完成的everyone = now.initialize(server)
(代码在这里 https://github.com/Flotype/now/blob/master/lib/now.js#L163).
初始化函数的作用是包装你的服务器 https://github.com/Flotype/now/blob/master/lib/now.js#L181与fileServer
(代码在这里 https://github.com/Flotype/now/blob/master/lib/fileServer.js) NowJS 中的类。这增加了一个nowjs“文件夹”下的资源 https://github.com/Flotype/now/blob/master/lib/fileServer.js#L36为客户服务now.js https://github.com/Flotype/now/blob/master/lib/client/now.js file.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)