now.js

编程入门 行业动态 更新时间:2024-10-23 13:30:14
now.js - Hello World示例 - “require not defined”(now.js - Hello World example - “require not defined”)

我无法让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.

更多推荐

本文发布于:2023-08-04 08:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!