Here is client side code that sends a fetch request to the /loginCheck route.
console.log("In user login checking function");
readAllData('loginDetail')
.then(function(loginDetails){
console.log(JSON.stringify(loginDetails[0]));
return fetch('/loginCheck', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(loginDetails[0])
})
.then(function(res){
console.log(res.status);
})
.catch(function(err){
console.log(err);
})
});
Here is the server side code that checks if the user is logged in and if he is i want to redirect to the main.html page.
app.post('/loginCheck', (req, res)=>{
const userName=req.body.nickName;
console.log(userName);
Detail.findOne({nickName:userName}, function(err, docs){
if(err){
console.log(err);
}else{
console.log("Inside else");
if(!docs){
res.redirect('/');
}else{
isLogged=true;
console.log(isLogged);
console.log("Redirecting to the main page");
res.redirect('/main.html');
}
}
})
});
The log statements are getting executed but the redirecting is not working.Could Anyone please point whats going wrong?