首页 > 解决方案 > Heroku 在我的本地服务器上运行时崩溃

问题描述

我正在尝试使用 Heroku 运行一个小型 HTTPS 服务器,但它只是给了我这个错误:

at=error code=H13 desc="Connection closed without response" method=GET path="/" host=###.herokuapp.com request_id=### fwd="###" dyno=web.1 connect=0ms service=6ms status=503 bytes=0 protocol=https

我的服务器如下所示:

let https = require("https");
let port = process.env.PORT;
if (port == null || port == "") {
    console.log("Using port 80");
    port = 8000;
} else {
    console.log("Using port supplied by Heroku");
    port = Number(port);
}

console.log(`Listening on port ${port}`);
const options = {
    key: [my key],
    cert: [my cert]
};
 
https.createServer(options, (request, response) => {
    console.log("Request recieved")
    response.writeHead(200);
    response.write("<!DOCTYPE html><head></head><body>hello world</body>")
    response.end();
}).listen(port);

我在本地运行它没有任何问题heroku local web。为什么会发生这种情况,我该如何解决?

编辑:原来你必须每月向 Heroku 支付 25 美元才能获得 HTTPS。看到这个答案

标签: javascriptwebherokuhttps

解决方案


我的服务器如下所示:

let https = require("https");

您不应该在 Heroku 上的应用程序代码中处理 HTTPS(或者可能在其他托管环境中,但让我们专注于您无法处理的 Heroku )。

相反,只需运行一个常规 HTTP 服务器,让 Heroku 将 HTTPS 流量路由到您的应用程序。对于 Node.js,通常看起来像这样(使用 Express;从Heroku 的 Node.js 入门指南简化):

const PORT = process.env.PORT || 5000

express()
  .listen(PORT, () => console.log(`Listening on ${ PORT }`))

如果要将 HTTP 重定向到 HTTPS,可以安装类似express-to-https.


推荐阅读