2012-08-13 64 views
0

這裏引用是我的設置:試圖創建node.js的自定義模塊,並從另一個文件

  • 託管Cloud9 IDE
  • Heroku的主機環境
  • 渴望學習的node.js

我正在通過教程發現在http://www.nodebeginner.org

曼努埃爾顯示如何創建EA自定義模塊叫server.js:

var http = require("http"); 

function start() { 
    function onRequest(request, response) { 
    console.log("Request received."); 
response.writeHead(200, {"Content-Type": "text/plain"}); 
response.write("Hello World"); 
response.end(); 
} 

http.createServer(onRequest).listen(8888); 
console.log("Server has started."); 
} 

exports.start = start; 

他演示如何引用從index.js這個自定義模塊:

var server = require("./server"); 
server.start(); 

我試圖在CLOUD9 IDE運行index.js但它掛起或給我一個服務暫時不可用的錯誤。我認爲這可能是cloud9的問題,所以我把它推到我的Heroku實例。從Heroku,我收到一個應用程序錯誤頁面。 Heroku的日誌顯示:

←[33m2012-08-13T19:03:19+00:00 heroku[slugc]:←[0m Slug compilation finished 
←[35m2012-08-13T19:03:21+00:00 heroku[web.1]:←[0m Starting process with command `node index.js` 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Object.<anonymous> (/app/index.js:1:70) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Module._compile (module.js:446:26) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Object..js (module.js:464:10) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead. 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Function.<anonymous> (module.js:383:11) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Module.load (module.js:353:31) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Function._load (module.js:311:12) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at Array.0 (module.js:484:10) 
←[35m2012-08-13T19:03:21+00:00 app[web.1]:←[0m  at EventEmitter._tickCallback(node.js:190:38) 
←[35m2012-08-13T19:03:23+00:00 heroku[web.1]:←[0m Process exited with status 1 
←[35m2012-08-13T19:03:23+00:00 heroku[web.1]:←[0m State changed from starting to crashed 

我GOOGLE了一下,發現與本地CLOUD9安裝的問題,但沒有對託管版本。我也嘗試將server.js從根目錄移動到node_modules文件夾,但似乎無法使其工作。我的第一步是讓標準的「Hello World」應用程序啓動並運行,以證明調試模式和heroku部署,並且它一切正常。這是我遇到困難的自定義模塊。

任何幫助表示讚賞。謝謝!

回答

1

它不在Cloud9上運行的原因是因爲您聽取端口8888而不是process.env.PORT

您顯示的heroku錯誤是一種不同類型的錯誤,它指出刪除節點0.4.x中已刪除的require.paths函數。所以這是完全不同的。你確定你正在看正確的日誌文件嗎?無論如何,將端口更改爲process.env.PORT應該有效。

如果你想看到codez,我把它們放在Cloud9項目上:webserver

+0

謝謝Jan,當我在Cloud9上輸入代碼時,我確實使用了process.env.PORT。 var http = require(「http」); function start(){ ** var port = process.env.PORT || 8888; ** 功能withBasicRequestHandler(請求,響應){ console.log(「Request received。」); response.writeHead(200,{「Content-Type」:「text/plain」}); response.write(「Hello World」); response.end(); } http.createServer(withBasicRequestHandler).listen(** port **); console.log(「Server has started。」); } exports.start = start; – Erds 2012-08-14 14:38:23

+0

你能給我一個鏈接到你的項目?我可以和你合作。或者郵寄給jan @ c9 – 2012-08-14 14:55:35

+0

好吧,我使用了你的代碼,並添加了一個新的包。json文件和一個新的Procfile,並將所有其他東西都刪除(刪除了node_modules文件夾),現在它可以在cloud9以及heroku中運行。我不知道什麼是錯,但真的很感謝幫助 – Erds 2012-08-14 18:08:21

相關問題