Home > front end >  Trying to make a redirect page
Trying to make a redirect page

Time:12-25

I am creating a new user and saving it in my DB as shown below:

const user= new User({
    username: userId,
    password: pass,
    nameOfUser: user_name,
    emailOfUser: user_email
);
user.save();

res.redirect("/redirectpage");
setTimeout(function(){
    res.redirect("/");
},5000);

After saving the user I am trying to create a countdown redirect page. It looks something like:

enter image description here

After this, counter (of 5 seconds) reaches 0, I intend to redirect the user to the login page which is the home route. I implemented this through setTimeout. But unfortunately my app crashes giving the following errors: (** Note: If i comment out res.redirect("/redirectpage"), no errors are shown)

enter image description here

CodePudding user response:

This code tries to redirect the user twice. First to /redirectpage and then again to /. You can't do this, because calling res.redirect() the first time finishes the request, so you can't send any more data like a second redirect. You can fix this by moving the redirect code to the client side javascript. This code should work:

setTimeout(function() {
    window.location.replace("[full redirect url]");
}, 5000);

Read this answer to learn more about redirecting from the client-side.

  • Related