首页 > 解决方案 > 如何获取传入的状态响应?

问题描述

服务器完成工作后如何获取传入响应?(节点新手)正如您将在下面的代码中看到的那样,在我的中间件向用户发送电子邮件后,我正在向前端发送响应。通过这个我想知道服务器完成了它的工作,所以我可以向用户展示他可以去查看他的电子邮件!任何想法如何实现这个!如果我发送 JSON 页面会重新加载如果我发送状态我不知道如何获得确认我可以通过发送 cookie 来做到这一点,但这是个好主意?

感谢您提前付出的努力和时间

// Backend signup route "/sign"
//SignUp New User logic 
exports.signUp = async (req, res, next) => {
    const newUser = await User.create({
        Email: req.body.Email,
        Password: req.body.Password,
        Password: req.body.PasswordConfirm
    })
    await sendEmail({
        email: req.body.Email,
        subject: 'Welcome new user',
    })
    //res.cookie("server has sent the email")
    //res.status(200)
    //next() 
}

// Frontend Response 
let request = fetch("/sign");
request
  .then(response => {
    const status = response.headers.get("status");
    if (status == 401) {
      // read 401 response
      response.text().then(res => console.log(res));
      return "404.html"
    }
    if (status == 200) {
      return "200.html"
    }
  })
  .then(result => console.log(result))
  //.catch(err => // handle error);

标签: javascriptnode.jsexpress

解决方案


推荐阅读