2012-08-13 59 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

相关问题