I have made a login system in my node JS server, and I want to send the client to another page if they are logged in, or if they are not. Example: if they try to access index.html while not logged in, I want to send them to login.html. I have tried res.redirect but it does not seem to work.
index.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="script.js"></script>
<script>
window.load = function (){
check();
}
</script>
<title>Document</title>
</head>
<body>
<h1>You should not see this without being logged in</h1>
</body>
</html>
script.js
function check() {
fetch(`http://127.0.0.1:4000/test_site`, {method: 'POST', body:JSON.stringify("test")})
.then((response) => response.text())
.then((data) => console.log(data))
}
server.js
const express = require("express");
const bodyParser = require("body-parser")
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.post("/test_site", async (req, res) => {
return res.redirect("http://127.0.0.1:5500/server/website/login.html");
}); // Does not redirect to login.html
app.listen(4000);
login.html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>Login page</h1>
</body>
</html>