私はAngularjsのウェブサイトを持っていて、すべてのHttp
トラフィックをHttps
にルーティングしようとしています。そのために、以下のコードを書いていますが、私のウェブサイトを開設するたびにHttp
のリダイレクトは起こりません。Nodejs res.redirectが機能しませんか?
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');
});
誰でもこの機能が動作しない理由を教えてください。
編集 - 私は、このミドルウェアはnot getting called
あるmost of the times
その後、いつでも私は私のウェブサイトを開封しておりますことに気づきました。どのようにこれを可能にすることができますか?
編集 - 私は私のindex.html
に私のセットbase href="/"
を持って、それはすべての問題が発生します。
'return next();'を削除します。 –
なぜあなたは 'return next()'が働いていないのかを説明できますか? –
@AikonMogwaiいいえ、まだ動作していません。 –