I’ve searched for this question for days and already asked it, but everything I’ve done hasn’t helped. When I’m redirecting from a POST request in node, nothing happens in the browser.
My app is configured like this:
./
config/
dbConnections.js
controller/
accountsController.js
userController.js
middleware/
validateToken.js
models/
movieModel.js
userModel.js
public/
index.html
userpage.html
routes/
accounts.js
users.js
.env
I’m redirecting from this function in controller/userController.js
:
const login = asyncHandler (async (req, res) => {
const {email, password} = req.body;
if (!email || !password){
res.status(400);
throw new Error("Email or password not provided");
}
const user = await User.findOne({email});
if (user && (await bcrypt.compare(password, user.password))) {
const accessToken = jwt.sign({
user: {
username: user.id,
email: user.email,
id: user.id
},
}, process.env.ACCESS_TOKEN_SECRET,
{expiresIn: "30m"});
console.log("Logged in!");
res.redirect(302, "/home/userPAGE");
}
else {
res.status(400);
throw new Error("Email or Password is not valid");
}
});
It just checks a user login, similar to other posts here, but it is also async which I thought is maybe why it’s not working at this point.
In index.js is where the function I’m redirecting to is.
index.js:
const express = require("express");
const app = express();
const dotenv = require("dotenv").config();
const port = process.env.PORT;
const path = require("path");
const http = require('http');
var engine = require('consolidate');
const ejs = require('ejs');
const mustache = require('mustache');
const fs = require('fs');
const db = require("./config/dbConnection.js");
db();
app.set('views', __dirname + '/views');
app.engine('html', engine.mustache);
app.set('view engine', 'html');
app.use(express.json());
app.use("/home/users", require("./routes/users"));
app.use("/home/user", require("./routes/accounts.js"));
app.get("/home/userTEST", (req, res) => {
res.status(200).sendFile(path.join(__dirname, "./views/index.html"));
});
app.get("/home/userPAGE", (req, res) => {
console.log("HERE")
res.render(path.join("userpage.html"));
});
app.listen(port, () =>{
console.log(`Server running on port ${port}`);
});
This is a picture of what happens in the network tab.
I was also using sendFile before, but tried to use mustache to see if anything would change.
Lastly, when redirecting I start on the /home/userTEST path (in index.js) and call the login function in the section of javascript:
<html>
<head>
</head>
<body>
<label> Email <input type="text" class="email" id = "email"/> </label> <br/>
<label> Password <input type="text" class="Password" id = "Password"/> </label> <br/>
<button> Submit </button>
</body>
<script>
var myEmail = document.querySelector(".email");
var myPassword = document.querySelector(".Password")
var button = document.querySelector("button")
button.addEventListener("click", ()=>{
var obj={
email:myEmail.value,
password:myPassword.value
};
fetch("/home/users/login", {
method:"POST",
headers:{
"Content-type":"application/json"
},
body:JSON.stringify(obj)
})
})
</script>
</html>