res.redirect 没有重定向到任何地方

res.redirect is not redirecting anywhere

像我的大多数问题一样,我看到了类似的问题,但它们的解决方案不同。我的 res.redirect simple 什么都不做。该页面保持不变,绝对不会尝试重定向。

我曾尝试将 res.redirect 放在其他位置,但后来出现错误。我只是想让页面在登录后重定向到另一个页面。

app.post('/login', function (req, res) {
  var usernameCollection = mongoDBDatabase.collection('accounts');
  var username = req.body.username;
  var enteredPassword = req.body.password;

  usernameCollection.findOne({
    $or:[
          { username: username}
        ]
    }).then(function(user){
      if (user) {
        console.log('That username was found in the database');
        bcrypt.compare(enteredPassword, user.password.substr(1), function(err, result){
          if(result == true){
              console.log('Password matches!');
              console.log('logged in as ' + username);
              usercurrentlyloggedin = username;
              res.redirect('/username');
          }
          else{
            console.log('Password did not match');
             res.redirect('/');
          }
        });
      }
      else{
        console.log('The username ' + username + ' was NOT found in     the database');
      }
   });
});

服务器端代码:

function login(){
  console.log("login: " + usernameField.value, pwField.value);
  console.log('login button clicked');

  var postRequest = new XMLHttpRequest();
  var requestURL = '/login';
  postRequest.open('POST', requestURL);

  var requestBody = JSON.stringify({
    personId: usernameField.value,
    username: usernameField.value,
    password: pwField.value
  });

  postRequest.setRequestHeader('Content-Type', 'application/json');
  postRequest.send(requestBody);

  usernameField.value= "";
  pwField.value = "";

}

当您最终向您发送客户端代码时,让我们修复双方:

想法是向服务器发送 json 负载并从服务器获得 json 响应并根据状态代码采取行动。

因此服务器端部分不会 return 重定向 - 客户端会自行重定向。

1) 服务器端

app.post('/login', async (req, res) => {
  try {
    const collection = mongoDBDatabase.collection('accounts');
    const {username, password} = req.body;

    const user = await collection.findOne({username});
    if (
      user &&
      await bcrypt.compare(password, user.password.substr(1))
    ) {
      res.status(200).send({});
      return;
    }

    res.status(403).send({message: 'Access denied'});
  }
  catch (error) {
    console.log(error);
    res.status(500).send({error: error.message});
  }
});

2) 客户端

function login(){
  console.log('login:',  usernameField.value, pwField.value);
  console.log('login button clicked');

  // request initialization
  var request = new XMLHttpRequest();
  request.setRequestHeader('Content-Type', 'application/json');
  request.responseType = 'json';
  request.open('POST', '/login');
  request.send(
    JSON.stringify({
      username: usernameField.value,
      password: pwField.value
    })
  );

  // request handling
  request.onload = function(e) {
    // success, redirecting user
    if (this.status == 200) {
      window.location.href = '/username';  
      return;
    }

    // got 403 from server, credentials are invalid
    if (this.status == 403) {
      alert('Username and/or password invalid.');
      return;
    }

    // system error raise
    if (this.status == 500) {
      alert('System error. Contact support.');
      return;
    }
  };
}