javascript - 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。看到这个答案。
解决方案
我的服务器如下所示:
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
.
推荐阅读
- android - 如何在laravel 5.7中使用retrofit send to api从android创建护照令牌,还有比护照更好的选择来进行身份验证
- tensorflow - TensorBoard 上(正确的)ANN 偏差和权重是什么样的?
- javascript - React Native Firebase:从firebase读取数据时出错
- javascript - How can I set value to componentDidMount from another function in React?
- c - 从 C 中获取 *ABS* 符号的值
- codenameone - 代号一透支
- c# - C# - 当使用out关键字传递引用类型时,调用函数代码可以控制外部对象吗?
- java - Spring 云服务器未从 Vault 后端访问所有配置文件
- python - 错误:TypeError:__init__() 接受 1 个位置参数,但给出了 2 个
- python - 如何在 python 中使用“with”从 mysql.connector.pooling 打开连接?