我无法让now.js聊天客户端教程工作。 (我也几乎完全按照这个视频 )。
server.coffee:
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)index.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 defined” 因此,客户端代码无法找到变量'now'。
我知道'require'是一个节点函数,但我如何让客户理解它?
任何帮助将不胜感激。
I'm having trouble getting the now.js chat client tutorial to work. (I've also followed this video almost exactly).
server.coffee:
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)index.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>When I load up the server with node server.js,
I get an error that says "require not defined" on line 1 of now.js. Consequently, the client side code can't find the variable 'now'.
I understand that 'require' is a node function, but how do I get the client to understand that?
Any help will be appreciated.
最满意答案
您在客户端源代码中包含的文件( ../Flotype/now/master/lib/now.js )是调用now = require 'now' ../Flotype/now/master/lib/now.js now = require 'now'时节点进程中包含的节点服务器端代码。
因此,将包含的客户端源文件从.../Flotype/now/master/lib/now.js为/nowjs/now.js将解决您的问题。
这个/nowjs/now.js文件来自哪里?
使用NowJS(和许多其他进行客户端/服务器通信的npm包)时,您可以扩展服务器对象。 这是通过行everyone = now.initialize(server) ( Code Here )来完成的。
初始化函数的作用是使用fileServer ( Code Here )类包装您的服务器 。 这会在“folder”nowjs下添加一个资源,该资源为client.js文件提供服务。
The file you're including in your client source (../Flotype/now/master/lib/now.js) is the Node server side code that is included in your node process when calling now = require 'now'.
So changing your included client source file from .../Flotype/now/master/lib/now.js to /nowjs/now.js will fix your problem.
Where does this /nowjs/now.js file come from?
When using NowJS (and many other npm packages that do client/server communication) you extend the server object. This is done with the line everyone = now.initialize(server) (Code Here).
What the initialize function does is wrap your server with the fileServer (Code Here) class in NowJS. This adds a resource under the "folder" nowjs which serves the client now.js file.
更多推荐
发布评论