Beginner
Beginner

Reputation: 9095

redirecting url not working in localhost - node js

I have a written a service to check whether the user is already logged in or not, but here i am not able to run the code in localhost. Why i am not able to run the below code in localhost.

The redirect is not working, to redirect to the home url it should be res.redirect("/") isn't ?

function checkUser(req,res){
  // checking cookie exists
  let authenticatedUser = checkCookieExist(req.headers.cookie, "username");
  if(authenticatedUser){
    res.status(200).redirect("/home")
  }else{
    // redirect to login page, once its success redirect it to root url
    res.status(200).redirect("/")
  }

}

Upvotes: 0

Views: 1201

Answers (1)

Mike Doe
Mike Doe

Reputation: 17566

You set invalid response code. In order for the redirect to work, it has to be one of the 3XX. Usually 302 for a temporary redirect, eg.:

res.status(302).redirect("/")

This is done by default, so simple would do:

res.redirect("/")

Upvotes: 3

Related Questions