2

node.js と now.js を正常にインストールしました。

now.js については、次のようにしました。

npm install now -g
npm install now (had to add this one. Without it, I get a "Cannot find now..." error message)

ノードサーバーを起動し、次のような server.js ファイルを提供すると:

var httpServer = require('http');
httpServer.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Node is ok');
res.end();
}).listen(8080);
console.log('Server runs on http://xxxxx:8080/');

すべて順調。

今、このファイルに now.js の基本的な使い方を追加しようとしています。

var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);

everyone.now.logStuff = function(msg){
    console.log(msg);
}

同じフォルダーに index.html ファイルを作成します (テスト目的)。

<script type="text/javascript" src="nowjs/now.js"></script>

<script type="text/javascript">
  now.ready(function(){
    now.logStuff("Now is ok");
  });
</script>

今回は、サーバーを起動したときに端末に表示されるのは次のとおりです。

Server runs on http://xxxxx:8080/

[TypeError: Object #<Object> has no method 'listeners']
TypeError: Object #<Object> has no method 'listeners'
    at Object.wrapServer (/home/xxxx/node_modules/now/lib/fileServer.js:23:29)
    at [object Object].initialize (/home/xxxx/node_modules/now/lib/now.js:181:14)
    at Object.<anonymous> (/home/xxxx/server.js:10:22)
    at Module._compile (module.js:444:26)
    at Object..js (module.js:462:10)
    at Module.load (module.js:351:32)
    at Function._load (module.js:309:12)
    at module.js:482:10
    at EventEmitter._tickCallback (node.js:245:11)

全くの初心者ですのでご了承ください。

ご協力ありがとうございました

4

1 に答える 1

1

'npm install -g'は、モジュールをグローバルレベルでインストールします。多くの場合、端末で使用するためのシステム全体のバイナリを提供することを目的としています。RubyGemsを考えてみてください。プロジェクトの一部としてモジュールを含める場合は、-gを削除する必要があります。

また、httpServer変数はサーバーではなく、httpモジュールです。createServer()は、nowjs.initialize()メソッドで使用する変数を使用してキャプチャするサーバーオブジェクトを次のように返します。

var http  = require('http')
    , now = require('now')

// Returns an Http Server which can now be referenced as 'app' from now on
var app = http.createServer(
    //... blah blah blah
)

// listen() doesn't return a server object so don't pass this method call
//     as the parameter to the initialize method below
app.listen(8080, function () {
    console.log('Server listening on port %d', app.address().port)
})

// Initialize NowJS with the Http Server object as intended
var everyone = nowjs.initialize(app)
于 2012-06-16T20:40:57.687 に答える