向服务器获取请求后,Express服务器“无法加载资源:服务器以500状态响应”如何解决此错误?

问题描述

用户输入他们的电子邮件密码,然后单击“登录”时,将向我的服务器发出提取请求:

const RegistrationForm = (props) => {
const [email,setEmail] = useState("");
const [password,setPassword] = useState("");
const handleLoginSuccess = (props) => {
return props.history.push("./homepage");
};

const handleSubmit = (e) => {
e.preventDefault();

authApiService
  .userLogin({ email,password })
  .then((res) => {
    password.value = "";
    TokenService.saveAuthToken(res.authToken);
    handleLoginSuccess();
  })
  .catch((res) => {
    console.log(res.error);
  });
};

return (
<form onSubmit={handleSubmit}>
  <fieldset>
    <div className="form-group">
      <div>
        <label htmlFor="registration-email">Email</label>
      </div>
      <EmailInput
        value={email}
        handleChange={(e) => setEmail(e.target.value)}
      />
    </div>

    <div className="form-group">
      <div>
        <label htmlFor="registration-password">Password</label>
      </div>
      <PasswordInput
        value={password}
        handleChange={(e) => setPassword(e.target.value)}
      />
    </div>
  </fieldset>
  <Button type="submit" theme="registration-button">
    Log in
  </Button>
  <ul>
    <li>
      <Link to="/register-account">Create account</Link>
    </li>
  </ul>
</form>
);
}; 

在此处提出获取请求:

userLogin({ email,password }) {
return fetch(`${config.API_ENDPOINT}/auth/login`,{
  method: "POST",headers: {
    "content-type": "application/json",},body: JSON.stringify({ email,password }),})
  .then((res) => {
    !res.ok ? res.json().then((e) => Promise.reject(e)) : res.json();
  })
  .then((res) => {
    TokenService.saveAuthToken(res.authToken);
    IdleService.registerIdleTimerResets();
    TokenService.queueCallbackBeforeExpiry(() => {
      authApiService.postRefreshToken();
    });
    return res;
  });
  },postRefreshToken() {
  return fetch(`${config.API_ENDPOINT}/auth/refresh`,headers: {
    authorization: `Bearer ${TokenService.getAuthToken()}`,})
  .then((res) =>
    !res.ok ? res.json().then((e) => Promise.reject(e)) : res.json()
  )
  .then((res) => {
    TokenService.saveAuthToken(res.authToken);
    TokenService.queueCallbackBeforeExpiry(() => {
      authApiService.postRefreshToken();
    });
    return res;
   })
   .catch((err) => {
     console.log("refresh token req error");
    console.log(err);
    });
 },

然后在服务器上,这是此请求的路由:

    authRouter.post("/login",jsonBodyParser,(req,res,next) => {
   const { email,password } = req.body;
  const userLoggingIn = { email,password };

  for (const [key,value] of Object.entries(userLoggingIn))
    if (value == null)
     return res
    .status(400)
    .json({ error: `Missing '${key}' in request body` });
  authService
   .confirmUserNameExists(req.app.get("db"),userLoggingIn.email)
    .then((userInDb) => {
     if (!userInDb)
    return res
      .status(400)
      .json({ error: "Incorrect email or password" });
     });
  return authService
   .comparePasswords(userLoggingIn.password,userInDb.password)
   .then((matchedPw) => {
    if (!matchedPw)
     return res
      .status(400)
      .json({ error: "Incorrect email or password" });

  const subject = userInDb.email;
  const payload = { userId: userInDb.id };
  res.send({ authToken: authService.createJwt(subject,payload) });
   })
   .catch(next);
 });

 authRouter.post("/refresh",requiresAuthorization,res) => {
  const subject = req.user.email;
  const payload = { userId: req.user.id };
  res.send({
    authToken: authService.createJwt(subject,payload),});
 });

我遇到标题错误(500错误,无法加载资源并将其指向提取请求。我尝试使用Google谷歌搜索,并在堆栈溢出中查找了数小时,无法解决。任何帮助都将是我们的目标是使用户登录,将用户重定向到主页,创建JWT并存储它。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)