重定向Node.js将静态请求表达到https [英] Redirect nodejs express static request to https
本文介绍了重定向Node.js将静态请求表达到https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将所有http请求重定向到https,包括对静态文件的请求.
I need to redirect all http requests to https including request to static files.
我的代码:
app.use(express.static(__dirname + '/public'));
app.get('*', function(req, res) {
if (!req.secure){
return res.redirect('https://' + config.domain + ":" + config.httpsPort + req.originalUrl);
}
res.sendFile(__dirname + '/public/index.html');
});
并且重定向不适用于静态文件.如果我更改订单:
And redirect not working on static files. If I change order:
app.get(...);
app.use(...);
然后我的静电不起作用.如何重定向此类请求?
Then my static not working. How to redirect on such requests?
推荐答案
var app = express();
app.all('*', function(req, res, next){
console.log('req start: ',req.secure, req.hostname, req.url, app.get('port'));
if (req.secure) {
return next();
}
res.redirect('https://'+req.hostname + ':' + app.get('secPort') + req.url);
});
这篇关于重定向Node.js将静态请求表达到https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文