3

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.

4

2 に答える 2

0

node の代わりに js コマンドを使用して nodejs ファイルを実行しようとすると、このエラーが発生しました。例:nodejsファイル名がtest.jsの場合、私はやっていました

js test.js

ノード test.js の代わりに

これが、このエラーを探している人々にも役立つことを願っています。

于 2014-06-21T09:57:47.587 に答える