我對節點使用expressjs並同時運行https和http。node.js expressjs模式匹配不等於
我想要求/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/*
的路線除外?
謝謝!
在第二個重定向中,你不應該只將非'/ secure/*'請求重定向到'http://'嗎? –