【发布时间】:2015-05-24 14:11:56
【问题描述】:
目前我让我的 node.js 项目同时创建 http 和 https 服务器。
var httpServer = http.createServer(app);
httpServer.listen(3000);
var httpsOptions = {
ca: fs.readFileSync(config.https.ssl.ca, 'utf8'),
key: fs.readFileSync(config.https.ssl.key, 'utf8'),
cert: fs.readFileSync(config.https.ssl.cert, 'utf8')
};
var httpsServer = https.createServer(httpsOptions, app);
httpsServer.listen(8000);
我还使用此中间件将所有 http 流量重定向到 https。
app.all('*', function(req, res, next){
var host = req.header("host");
if (host && host.indexOf('localhost') !== -1) {
next()
} else if (req.connection.encrypted) {
next();
} else {
res.redirect('https://' + host + req.url);
}
});
但对于某些页面我不需要 https 连接,例如 http://www.domain.com/shops/ 路由。我可以让这条路线使用http方法,而所有其他路线仍然使用https吗?
p.s:此页面从其他路由请求资源,如 bower_components、public 等。
【问题讨论】: