gpt4 book ai didi

node.js - Console.log 无法与 heroku 日志一起按预期工作

转载 作者:行者123 更新时间:2023-12-04 22:36:56 25 4
gpt4 key购买 nike

我的代码:

app.get('*', (req, res) => {
console.log(req.headers);

// if (req.headers['protocol'] != 'https') {
// res.redirect('https://www.soundsqueezr.com/' + req.url);
// }
});

app.get("/", (req, res) => {
res.sendFile(staticPath);
});
我正在尝试查看从我的第一个 node+express+heroku 应用程序上的请求发送的 header ,但我找不到 console.log(req.headers);显示。当在本地运行并从 localhost 查看时,我可以看到输出到终端的控制台日志,但从 heroku 日志中我只能看到一堆这样的:
2022-01-02T20:40:20.248597+00:00 heroku[router]: at=info method=GET path="/background.jpeg" host=www.soundsqueezr.com request_id=16775427-28b9-4027-bdea-e555dcf42ee2 fwd="24.201.224.174" dyno=web.1 connect=0ms service=2ms status=304 bytes=324 protocol=http
最终目标是通过检查请求中的 header 是 http 还是 https 并将用户重定向到我的应用程序域地址的 https 版本,如果它是 http 则重定向。但我遇到了一些麻烦。任何帮助是极大的赞赏

最佳答案

您遇到的问题实际上是由于您访问标题的方式。您可以使用 protocol 访问您要查找的内容。
尝试这个:

app.get('*', (req, res) => {
let protocol = req.protocol

if (protocol != 'https') {
res.redirect('https://www.soundsqueezr.com/' + req.url);
}
});

app.get("/", (req, res) => {
res.sendFile(staticPath);
});

关于node.js - Console.log 无法与 heroku 日志一起按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70559687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com