now.js:尝试启动server.js时,“对象没有方法”错误消息

我成功安装了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); } 

我在同一个文件夹中创build一个index.html文件(用于testing目的)

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

这一次,这是我在启动服务器时在terminal上得到的东西:

 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) 

请记住,我是一个绝对的初学者。

感谢您的帮助

'npm install -g'在全球范围内安装模块,通常意图为terminal使用提供系统范围的二进制文件。 想想Rubygem。 如果你想包含一个模块作为你的项目的一部分,你需要删除-g。

另外,你的httpServervariables不是你的服务器,而是http模块。 createServer()返回一个服务器对象,你想要捕获一个variables来在你的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)