Reputation: 25
my code:
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);
});
I am trying to view the headers that are sent from the request on my first node+express+heroku app and I cannot find where the console.log(req.headers); is shown. When run locally and viewed from localhost I can see the console log outputted to the terminal, but from the heroku logs I can only see a bunch of this:
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
The end goal is to redirect the the user to the https version of my apps domain address by checking to see if the header in the request is http or https and redirecting if it is http. But I am having some trouble. Any help is greatly appreciated
Upvotes: 2
Views: 163
Reputation: 202
The problem you're encountering is actually due to the way you are accessing the headers. You can access what you're looking for by using protocol
try this:
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);
});
Upvotes: 1