如何在不使用Express的情况下将节点应用程序部署到heroku

时间:2018-02-25 19:01:25

标签: javascript node.js http heroku heroku-api

我已将我的node.js应用程序成功部署到heroku但是当我访问该站点时出现错误 "Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch"

我的代码是

var router = require("./router.js");

// Create a web server
var http = require("http");

http.createServer(function (request, response) {
  router.home(request, response);
  router.user(request, response);
}).listen(1337);
console.log("Sever running at http://127.0.0.1:1337/");

我认为它与端口有关。有一个类似于我的问题的帖子Heroku + node.js error (Web process failed to bind to $PORT within 60 seconds of launch) 但他们的解决方案基于Express。我没有为我的应用程序使用express。 有没有其他方法可以将端口设置为固定数字?

1 个答案:

答案 0 :(得分:1)

如果您使用express,则无关紧要。 Herouku在process.env.PORT环境变量中为您绑定了一个端口。您需要像这样使用它:

var port = process.env.PORT || 1337

然后使用.listen(port)
确保您连接到浏览器中的正确端口(将端口记录到控制台以确保)

相关问题