I encountered an issue while attempting to redirect with ExpressJS. I implemented a login system that processes a post request, and if the user exists, the page is supposed to redirect. However, Cors seems to be blocking the redirection.
Here is the code snippet for the request:
router.post('/login', async (req, res) => {
try{
let usersData = await getFiles(__dirname + '/users.json');
let parsedUsers = JSON.parse(usersData);
let userChecker;
for(let i = 0; i < parsedUsers.length; i++){
if(parsedUsers[i].userName === req.body.userName){
userChecker = 1;
break;
}
}
if(!userChecker){
console.log(`${req.body.userName} does not exist`);}
else {
console.log(`${req.body.userName} Approved`);
res.redirect('/')
}
}
catch (err) {
if(err) throw err
};
})
This is the error message displayed in the console:
Access to fetch at 'http://localhost:3001/users/login' (redirected from 'http://localhost:4000/users/login') from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Could someone shed some light on what might be causing this CORS problem?