javascript - 如何在不使用 Express 的情况下将 Node 应用程序部署到 heroku

标签 javascript node.js http heroku heroku-api

我成功地将我的node.js应用程序部署到heroku,但是当我访问该网站时出现错误 “错误 R10(启动超时)-> Web 进程未能在启动后 60 秒内绑定(bind)到 $PORT”

我的代码是

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。 有没有其他方法可以不将端口设置为固定号码?

最佳答案

是否使用express并不重要。 Herouku 在 process.env.PORT 环境变量中为您绑定(bind)了一个端口。您需要像这样使用它:

var port = process.env.PORT || 1337

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

关于javascript - 如何在不使用 Express 的情况下将 Node 应用程序部署到 heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48977268/

相关文章:

javascript - 验证 XHTML 中的脚本标记

javascript - React redux 路由器显示空白页

node.js - DynamoDB : ResourceNotFoundException When Creating Table (local)

android - 使用 Http Post 发送图像

javascript - 在对象上调用 forEach

Javascript 按钮,防止鼠标双击

node.js - "SyntaxError: Cannot use import statement outside a module"依赖项

node.js - 仅针对200或500个错误代码返回错误消息

javascript - 如何让我的获取错误显示 http 状态代码?

使用 clojure 在 Heroku 上进行 HTTP 流式处理/分块响应