当监听process.env.PORT时,heroku应用程序崩溃 [英] heroku app crash when listening on process.env.PORT
本文介绍了当监听process.env.PORT时,heroku应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在heroku上部署它时:
When i deploy this on heroku :
app.js
var port = process.env.PORT || 8080;
app.listen(port, function(){
console.log("Listening on port : " + port);
});
我得到了错误日志:
2014-11-08T18:05:34.300115+00:00 app[web.1]: events.js:72
2014-11-08T18:05:34.300402+00:00 app[web.1]: throw er; // Unhandled 'error' event
2014-11-08T18:05:34.300404+00:00 app[web.1]: ^
2014-11-08T18:05:34.303599+00:00 app[web.1]: Error: listen EADDRINUSE
2014-11-08T18:05:34.303605+00:00 app[web.1]: at errnoException (net.js:904:11)
2014-11-08T18:05:34.303607+00:00 app[web.1]: at Server._listen2 (net.js:1042:14)
2014-11-08T18:05:34.303608+00:00 app[web.1]: at listen (net.js:1064:10)
2014-11-08T18:05:34.303610+00:00 app[web.1]: at Server.listen (net.js:1138:5)
2014-11-08T18:05:34.303628+00:00 app[web.1]: at Function.app.listen (/app/node_modules/express/lib/application.js:556:24)
2014-11-08T18:05:34.303629+00:00 app[web.1]: at Object.<anonymous> (/app/bin/www:7:18)
2014-11-08T18:05:34.303630+00:00 app[web.1]: at Module._compile (module.js:456:26)
2014-11-08T18:05:34.303632+00:00 app[web.1]: at Object.Module._extensions..js (module.js:474:10)
2014-11-08T18:05:34.303633+00:00 app[web.1]: at Module.load (module.js:356:32)
2014-11-08T18:05:34.303635+00:00 app[web.1]: at Function.Module._load (module.js:312:12)
但是当我直接在8080端口上监听时,它可以工作,而且我不明白为什么:
but when i listen directly on port 8080 it work, and i cant understand why :
var port = 8080;
app.listen(port, function(){
console.log("Listening on port : " + port); //it work
});
我刚刚开始使用nodejs,但我真的不明白为什么它会崩溃.
I have just started using nodejs and I don't really understand why it crashes.
有人可以解释其原因或如何对其进行调试"吗?
Could someone either explain the cause of this or how to "debug" this?
谢谢!
推荐答案
好的,问题在于express-generator创建了一个包含以下内容的/bin/www文件:
OK, the problem was that express-generator create a /bin/www file that contain :
#!/usr/bin/env node
var debug = require('debug')('toto');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
这就是为什么该端口已被使用的原因.
Thats why the port was already used.
谢谢
这篇关于当监听process.env.PORT时,heroku应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文