Http traffic to Https and for that, I have written below code but it is not working, every time I am opening my website on Http redirect is not happening.var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public')); // Website part added
var bodyParser = require('body-parser')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }))
app.use(function(req,res,next) {
if(req.headers["x-forwarded-proto"] == "http") {
res.redirect("https://" + req.headers.host + req.url);
return next();
} else {
console.log('Request was not HTTP');
return next();
}
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.get('*', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
Can anyone please tell me why this is not working ??
Edit - I noticed that whenever I am opening my website then most of the times this middleware is not getting called. How can this be possible ??
Edit - I have my set base href="/" in my index.html will it cause any problem.