node.js - Redirected you too many times - Express/node on Heroku -
using express/node on heroku error: redirected many times or net::err_too_many_redirects in console.
i'm new express , tried add redirect http requests on 2 custom domains redirected https. what's breaking site. if has ideas fix amazing!
var express = require('express'); const path = require('path'); var app = express(); // process.env.port gets port heroku or goes 3000 const port = process.env.port || 3000; app.enable('trust proxy'); // in production on heroku - re-route https if (process.env.node_env==="production") { app.use((req, res, next) => { if (req.header['x-forwarded-proto'] !== 'https') { res.redirect('https://' + req.hostname + req.url); } else { next() } }) } app.use(express.static(path.join(__dirname, '/public'))); app.get('*', (req, res) => { res.sendfile(path.join(__dirname, 'public/index.html')); }); app.listen(port, function() { console.log('express server on port:' + port); });
use req.header('x-forwarded-proto')
instead.
and make sure, console.log process.env.node_env
, req.header('x-forwarded-proto')
, req.hostname
, req.url
see redirection works expected
Comments
Post a Comment