【问题标题】:send post respond in nodejs在节点 js 中发送帖子响应
【发布时间】:2018-09-24 10:40:59
【问题描述】:

我在注册过程中使用 bcrypt 加密密码..这是我的登录代码..我只想在密码和电子邮件正确的情况下发送回复。如果电子邮件错误,它会发出警报-"失败”..如果一切都正确,它会发出警报-“成功”。但是在此代码中,如果密码错误,它不会发送任何内容,我不能为此使用警报..我怎样才能发送没有任何内容的响应收到警报了吗?

这是我的 reactjs 代码.....

fetch("http://localhost:3000/reg/getuser",
{
    method:"POST",
    headers: {
        "Content-Type": "application/json"
    },
    body:JSON.stringify(user)
})
.then(function(response)
{
    return response.json();
})
.then(function(data,props)
{
    if(data.length == 0)
    {
        console.log("damn");
        window.alert('Login Failed!')
    }
    else
    {
        console.log("done");
        window.alert('Login Successful!');
    }
});

这是我的 nodejs 代码...

router.post('/getuser',function(req,res)
{
    Customer.find({email:req.body.email})
    .then(function(details){
        if(details.length<1)
        {
            res.send(details)
        }
        else
        {
            bcrypt.compare(req.body.password,details[0].password,(err,result)=>{
                if(err){
                    console.log(err)
                }

                if(result){
                    res.send(details)
                }
                // here,when password is wrong...want to send a respond as
                // data.length==0 (in fetch)
            });
        }
    });
});

【问题讨论】:

  • 请使用 StackOverflow 添加媒体资源或任何与问题相关的内容,第三方资源可能会在一段时间后失效。您应该仅使用第三方工具为无法放在 StackOverflow 本身上的问题提供更多参考和示例。
  • 欢迎堆栈溢出。这里的政策是,理解问题所需的任何代码都必须作为文本(不仅在外部链接上)粘贴到问题本身中,然后正确格式化为代码。这是因为外部资源有随时间变化或事件消失和堆栈溢出的习惯,成为长期可搜索的解决方案资源库。
  • 在你的 node.js 代码中没有 elseif (result) 案例。我猜这就是如果密码错误会执行什么?还是您的意思是如果密码错误,您会尝试发送空响应?如果 node.js 没有正确发送,我会感到非常惊讶 - 你能在浏览器调试工具中验证吗?
  • @Rup 是的。在其他情况下,如果(结果)我应该发送一个响应为 data.length==0..我该怎么做??
  • This site 说你可以用res.end(),但我不知道表达得足够好,无法评论,对不起。

标签: node.js reactjs mern


【解决方案1】:

在进行身份验证时,您应该使用正确的状态代码。 您可以使用res.status(200).send('loggedin') 设置您的状态码。

使用以下状态码:

200 - 表示ok登录成功

400401 - 表示身份验证失败。

要显示错误消息或重定向用户,请检查您的 ajax 请求中的状态代码并执行您的操作。

编辑修复客户端sn-p。

客户

fetch("http://localhost:3000/reg/getuser",
{
    method:"POST",
    headers: {
        "Content-Type": "application/json"
    },
    body:JSON.stringify(user)
})
.then(function(response)
{   
    if (response.status === 200) {
        console.log("ok");
        console.log(response.json());
        window.alert('Login successfull!')
    } else {
        console.log("damn");
        window.alert('Login Failed!')
    }
})
.catch(function() {
    console.log('error handling');
});

服务器

router.post('/getuser',function(req,res)
{
    Customer.find({email:req.body.email})
    .then(function(details){
        if(details.length<1)
        {
            res.status(400).send(details)
        }
        else
        {
            bcrypt.compare(req.body.password,details[0].password,(err,result)=>{
                if(err){
                    console.log(err)
                }

                if(result){
                    return res.status(200).send(details);
                    // return res.status(200).json(details); Use this line to send a json if result is an object.
                }

                return res.status(400).send('login failed');
            });
        }
    });
});

【讨论】:

  • 天啊..!!它运作良好..非常有帮助..非常感谢..!! :D :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 2018-10-23
  • 2014-07-21
  • 2015-10-20
相关资源
最近更新 更多