How to redirect another page after nodejs login successfully

I try to send the client to the next page upon successful completion of the login process, the page confirms the page. But I get some error. image screenshot

router.post('/sign_in', urlend, function(req, res) {
  var email = req.body.user_id;
  var password = req.body.password;
  if (email != '' && password != '') {
    user_modell.findOne({
      email: email,
      password: password
    }, function(err, data) {
      if (err) {
        //res.status(500).send();
        console.log('error');
      } else if (!data) {
        console.log('Incorrect User ID or Password');
        return res.end();
      } else {
        res.render("confirm");
      }
    });
  }
  res.end();
});
+4
source share
1 answer

response.redirect('URL'); used to redirect a request to another page

the code

router.post('/sign_in',urlend,function(req,res){
    var email=req.body.user_id;
    var password=req.body.password;
    if(email!='' && password!=''){      
        user_modell.findOne({email:email,password:password},function(err,data){
            if(err){
                //res.status(500).send();
                console.log('error');
            } else if(!data){
                console.log('Incorrect User ID or Password');
                return res.end();
            }else{
                  res.redirect("/confirm"); 
            }
        });
    }
    res.end();
});

You can use express-redirect package.

An explanation for your mistake

[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client , . model.findOne - -. , . , res.end() . .

, : - /, : " , ", /. , , , , , , !

+2

Source: https://habr.com/ru/post/1691006/


All Articles