2012-08-14 79 views
0

我試圖讓nowjs使用快遞3.0,就像在this問題一樣。我正在關注代碼,但我仍然得到「現在沒有定義」。Nowjs仍然沒有使用快遞3.0

我在Oracle VM虛擬框中運行Ubuntu 12.04。

首先我創建一個快速應用程序。

$ express myBrilliantNewApp 

然後,我安裝nowjs在新的目錄中。

$ sudo npm install now 

添加服務器變量並立即使用它。

var server = http.createServer(app).listen(app.get('port'), function() { 
console.log('Express server listening on port ' + app.get('port')); 
}) 

var everyone = require('now').initialize(server); 

然後,我應該能夠做到這一點...

everyone.now.writesomething = function() { 
console.log('I would really like it if this shows up at the console') 
}; 

...和撥打電話,並檢查什麼大家是怎麼一回事。

now.writesomething(); 
console.log(everyone); 

它不起作用。我得到:

[ReferenceError: now is not defined] 
ReferenceError: now is not defined 
at Object.<anonymous> (/home/jan/dev/apps/probeer1/app.js:44:1) 
at Module._compile (module.js:449:26) 
at Object.Module._extensions..js (module.js:467:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:312:12) 
at Module.runMain (module.js:492:10) 
at process.startup.processNextTick.process._tickCallback (node.js:244:9) 

誰能告訴我我在做什麼錯在這裏?

全面上市app.js

/** 
* Module dependencies. 
*/ 

var express = require('express') 
    , routes = require('./routes') 
    , http = require('http') 
    , path = require('path'); 

var app = express(); 

app.configure(function(){ 
    app.set('port', process.env.PORT || 3000); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.favicon()); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(path.join(__dirname, 'public'))); 
}); 

app.configure('development', function(){ 
    app.use(express.errorHandler()); 
}); 

app.get('/', routes.index); 

var server = http.createServer(app).listen(app.get('port'), function() { 
    console.log('Express server listening on port ' + app.get('port')); 
}) 

var everyone = require('now').initialize(server); 


everyone.now.writesomething = function() { 
    console.log('I would really like it if this shows up at the console') 
}; 

now.writesomething(); 
console.log(everyone); 

回答

0

在服務器上使用nowjs之前,你需要調用:

var nowjs = require('now'); 

要在客戶機上使用nowjs你需要在你的HTML的腳本參考:

<script src="/nowjs/now.js"></script> 
+0

日Thnx與我忘了代碼修修補補後再試一次,是我不好。放回去。然而,相同(種類)的問題 – 2012-08-14 05:28:10

+0

您是否在客戶端包含了now.js腳本引用?查看更新的答案。 – JohnnyHK 2012-08-14 15:20:31

+0

我試圖做一個乾淨的錯誤再現,從服務器端調用一個函數。查看更新的問題。這應該工作嗎? – 2012-08-14 21:41:44

0

看到這個答案的詳細信息:Cannot find module 'now' - nowjs and nodejs

npm config set global true && \ 
    echo 'export NODE_PATH="'$(npm root -g)'"' >> ~/.bashrc && \ 
    . ~/.bashrc 

與全局標誌

sudo npm install now -g 
+0

這似乎確實是問題,雖然NODE_PATH變量似乎確定。 echo $ NODE_PATH =>/usr/local/lib/node_modules。問題依然存在。我得到,檢查節點路徑:找不到,與npm安裝時。例如,嘗試使用dnode時出現同樣的錯誤。 – 2012-08-16 07:04:02

+0

我已經嘗試過全球和本地。不起作用。也許有與NODE_PATH env變量的用戶範圍的東西? – 2012-08-16 11:51:56

相關問題