9
0
mirror of https://github.com/Lomanic/presence-button-web synced 2024-11-26 07:27:30 +00:00

🌍 Checkpoint

./server.js:933874/382
This commit is contained in:
Glitch (hello-express) 2019-12-08 23:47:24 +00:00
parent 3de62c0b4b
commit 41f09e9473

View File

@ -22,6 +22,7 @@ try {
app.use(express.static("public")); app.use(express.static("public"));
app.enable("trust proxy"); // needed for HTTP -> HTTPS redirect and successful test against req.secure app.enable("trust proxy"); // needed for HTTP -> HTTPS redirect and successful test against req.secure
// redirect every route but /status as it's used by the ESP to send its status https://stackoverflow.com/a/49176816
const redirectToHTTPS = (req, res, next) => { const redirectToHTTPS = (req, res, next) => {
if (req.secure) { if (req.secure) {
// request was via https, so do no special handling // request was via https, so do no special handling
@ -31,11 +32,11 @@ const redirectToHTTPS = (req, res, next) => {
res.redirect("https://" + req.headers.host + req.url); res.redirect("https://" + req.headers.host + req.url);
} }
}; };
app.use("/", redirectToHTTPS);
app.use("/api", redirectToHTTPS);
app.use("/img", redirectToHTTPS);
app.get("/", (req, res) => { app.get("/", (req, res) => {
if (!req.secure) {
return res.redirect(["https://", req.get("Host"), req.baseUrl].join(""));
}
res.sendFile(__dirname + "/views/index.html"); res.sendFile(__dirname + "/views/index.html");
}); });