Node.js now.js:“我的名字叫什么?”;对象没有方法”;尝试启动server.js时出现错误消息

Node.js now.js:“我的名字叫什么?”;对象没有方法”;尝试启动server.js时出现错误消息,node.js,ubuntu-11.04,nowjs-sockets,Node.js,Ubuntu 11.04,Nowjs Sockets,我成功安装了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 (re

我成功安装了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文件(用于测试目的)


now.ready(函数(){
logStuff(“现在可以了”);
});
这一次,这是我在启动服务器时在终端上得到的结果:

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)
服务器在服务器上运行http://xxxxx:8080/
[TypeError:对象#没有方法“侦听器”]
TypeError:对象#没有方法“侦听器”
在Object.wrapServer(/home/xxxx/node_modules/now/lib/fileServer.js:23:29)
在[object object]处初始化(/home/xxxx/node_modules/now/lib/now.js:181:14)
反对。(/home/xxxx/server.js:10:22)
在模块处编译(Module.js:444:26)
at Object..js(module.js:462:10)
在Module.load(Module.js:351:32)
at功能。加载(module.js:309:12)
在module.js:482:10
在EventEmitter上进行回调(node.js:245:11)
请记住我是个绝对的初学者


感谢您的帮助

'npm install-g'在全局级别安装模块,目的通常是为终端使用提供系统范围的二进制文件。想想红宝石吧。如果要将模块作为项目的一部分,则需要删除-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)

几件事,1)最好不要使用-g标志安装,在项目中本地安装它们,最好使用package.json文件。2) 是否调用now.ready回调?3) 是否加载了nowjs/now.js?也许尝试一下/nowjs/now.js,那么您在服务器端遇到了这个错误?
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)