node.js - Heroku 的 Nodejs 端口错误

标签 node.js heroku

我正在尝试将我的应用程序推送到 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

您可以在此错误消息中看到应用程序正在使用正确的端口,但 heroku 仍然显示错误的绑定(bind)错误。感谢您的帮助。

最佳答案

Heroku will pass the port in a environment variable named PORT ,这意味着您可以通过 process.env 访问它。

只需更改您的代码以使用正确的端口调用listen

var port = process.env.PORT || 3000;
app.listen(port);

关于node.js - Heroku 的 Nodejs 端口错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7948913/

相关文章:

express - 使用 Vercel 作为前端,使用 Heroku 作为 api

node.js - mongoose findOne 返回找到的第一个对象,即使条件不成立

ruby-on-rails - 如何让我的数据库查询更有效率?

django - 在 Heroku 上为 Django 静态文件添加过期 header

javascript - 在 Lambda 中使用 child_process 时 AWS API Gateway 立即超时

email - 如何在 Heroku 上使用 Mandrill 处理入站电子邮件?

google-app-engine - Heroku - 旋转起来

javascript - nodejs如何将父对象的范围限定为mysql查询匿名回调

javascript - 如何使用 Node/Angular/JavaScript 将多个图像上传到亚马逊 AWS S3 存储桶

javascript - ReactJs 与 ChartJs 中的数据可视化