我正在使用带有节点的 expressjs 并同时运行 https 和 http。
我想要求所有路由都/secure/*
使用 https。这个完成了:
app.all("/secure/*", function(req, res, next) {
if (!req.connection.encrypted) {
res.redirect("https://" + req.headers["host"].replace(new RegExp(config.http_port, "g"), config.https_port) + req.url);
} else {
return next();
};
});
但是,我还想要求所有未使用/secure/*
并尝试访问 https 的路由都使用相同的方法重定向到 http。
我试过这样做:
app.all("*", function(req, res, next) {
console.log(req);
if (req.connection.encrypted) {
res.redirect("http://" + req.headers["host"].replace(new RegExp(config.https_port, "g"), config.http_port) + req.url);
} else {
return next();
};
});
但是在访问 https 页面时,我最终陷入了重定向循环。有没有办法指定所有路线,除了那些/secure/*
?
谢谢!