Heroku的Nodejs端口错误

Mut*_*amy 4 heroku node.js

我正在尝试将我的应用程序推送到heroku我遇到这个问题,我使用process.env.PORT变量使用正确的端口号但仍然收到此错误消息:

heroku[web.1]:  Starting process with command `node app.js`
app[web.1]:     info  - socket.io started
app[web.1]: Express server listening on port 49559 in development mode
heroku[web.1]:  Error R11 (Bad bind) -> Process bound to port 10843, should be 49559 (see environment variable PORT)
heroku[web.1]:  Stopping process with SIGKILL
heroku[web.1]:  Process exited
Run Code Online (Sandbox Code Playgroud)

您可以在此错误消息中看到应用程序正在使用正确的端口,但仍然heroku显示错误的绑定错误.任何帮助表示赞赏.

Jak*_*old 8

Heroku将在名为的环境变量中传递端口PORT,这意味着您可以通过它访问它process.env.

只需更改您的代码即可listen使用正确的端口进行呼叫

var port = process.env.PORT || 3000;
app.listen(port);
Run Code Online (Sandbox Code Playgroud)


小智 5

我确定您使用的是使用端口10843的闪存套接字,并且该端口不是分配给应用程序的端口,您需要设置:

io.configure(function () {
  io.set('transports', ['xhr-polling']);
  io.set("polling duration", 10); 
});
Run Code Online (Sandbox Code Playgroud)