AngularJs App engine res.redirect not working?

96 views
Skip to first unread message

Sudhanshu Gaur

unread,
Dec 3, 2017, 11:09:26 AM12/3/17
to Google App Engine
I have my Angularjs website and I am trying to route all my 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.


Kenworth (Google Cloud Platform)

unread,
Dec 3, 2017, 1:21:23 PM12/3/17
to Google App Engine
You have to uploaded first your SSL certificate and enable SSL security settings. You can follow this article on how to secure custom domains with SSL.
Reply all
Reply to author
Forward
0 new messages