2012-04-21 110 views
2

我成功安裝了node.js和now.js。now.js:嘗試啓動server.js時出現「Object has no method」錯誤消息

對於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) 

請記住我絕對是初學者。

謝謝您的幫助

+1

幾件事情:1)最好不要安裝使用-g標誌,安裝他們在本地項目中,最好使用package.json文件。 2)now.ready回調被調用了嗎? 3)現在是js/now.js加載?也許試試/nowjs/now.js。 – 2012-04-22 13:45:42

+0

所以你在服務器端收到這個錯誤? – 2012-04-25 03:40:47

回答

1

「故宮安裝-g」在全球範圍內安裝模塊,經常與終端使用提供全系統的二進制文件的意圖。想想Ruby寶石。如果你想包含一個模塊作爲你的項目的一部分,你需要刪除-g。

另外,你的httpServer變量不是你的服務器,而是http模塊。 createServer()返回要使用一個變量來捕捉你的nowjs.initialize(使用)的方法如下:a服務器對象:

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) 
相關問題