CoffeScript:从浏览器运行ninjg 时无法识别要求
我正在尝试运行这段取自 http://coffeescriptcookbook.com 的代码,将其嵌入到 html 中。
net = require 'net'
domain = 'localhost'
port = 9001
connecting = (socket) ->
console.log "Connecting to real-time server"
connection = net.createConnection port, domain
connection.on 'connect', () ->
console.log "Opened connection to #{domain}:#{port}"
connecting connection
connection.on 'data', (data) ->
console.log "Received: #{data}"
connection.on 'end', (data) ->
console.log "Connection closed"
此代码位于名为 client.coffe 的文件中,当我使用咖啡命令运行它时:coffee client.coffe 它运行良好并连接到服务器,但是当我将其嵌入到 html 文件中并打开它时,我收到此错误: Uncaught ReferenceError:未定义要求。
我的 html 脚本标签如下所示:
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"
type="text/javascript" charset="utf-8" ></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}functions.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}jquery.dajax.core.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}client.coffee"
type="text/coffeescript" charset="utf-8"></script>
有什么想法吗?
I am trying to run this piece of code taken from http://coffeescriptcookbook.com embedding it into an html.
net = require 'net'
domain = 'localhost'
port = 9001
connecting = (socket) ->
console.log "Connecting to real-time server"
connection = net.createConnection port, domain
connection.on 'connect', () ->
console.log "Opened connection to #{domain}:#{port}"
connecting connection
connection.on 'data', (data) ->
console.log "Received: #{data}"
connection.on 'end', (data) ->
console.log "Connection closed"
This code is in file named client.coffe and when i run it with the coffee command: coffee client.coffe it runs fine and connects to the server, but when I embbed it in a html file and open it i get this error: Uncaught ReferenceError: require is not defined.
My html script tags looks like this:
<script src="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"
type="text/javascript" charset="utf-8" ></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}functions.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}jquery.dajax.core.js"
type="text/javascript" charset="utf-8"></script>
<script src="{% get_static_prefix %}client.coffee"
type="text/coffeescript" charset="utf-8"></script>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在浏览器中不起作用。
第一个问题:出于安全原因,不允许浏览器中的内容连接到其来源以外的其他服务器或端口。而且,你得不到真正的套接字,只有 HTTP。
第二个问题:
require
是一个node.js命令,您只能在node.js中使用(也就是说,当您使用node
命令运行javascript文件时或带有coffee
命令的 Coffeescript 文件)。net
模块属于node.js,在浏览器中永远不会以这种方式工作。如果您想从浏览器内部实时与服务器对话,我推荐使用
socket.io
模块websockets、flashsockets 和 HTTP(这些可以在浏览器中使用)。This won't work in the browser.
First issue: Stuff in the browser isn't allowed to connect to other servers or ports than it's coming from for security reasons. Also, you don't get real sockets, just HTTP.
Second issue:
require
is a node.js command you'll only be able to use in node.js (that is, when you run a javascript file with thenode
command or a coffeescript file with thecoffee
command). Thenet
module belongs to node.js and will never work this way in the browser.If you want to talk to the server in realtime from inside the browser, I recommend the
socket.io
module which uses websockets, flashsockets and HTTP (those are usable from within the browser).您可以在具有 node-browserify 等包装器的浏览器中使用
require
。但是,@thejh 指出的所有问题都是正确的,因此您必须重新考虑您的代码。You can use
require
in a browser with wrappers like node-browserify. However, all problems pointed out by @thejh are correct, so you'll have to rethink your code.