Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何验证谷歌验证码_Node.js_Recaptcha_Verify - Fatal编程技术网

Node.js 如何验证谷歌验证码

Node.js 如何验证谷歌验证码,node.js,recaptcha,verify,Node.js,Recaptcha,Verify,客户端代码 <script src='https://www.google.com/recaptcha/api.js'></script> <script> var verified = function() { document.getElementById("loginform").submit(); }; </script> <form action="www.example.com/" method

客户端代码

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
   var verified = function() {
       document.getElementById("loginform").submit();
   };
</script>    

<form action="www.example.com/" method="POST" id="loginform" onsubmit=" return validation()">
   <input  id="email" maxlength="80" name="email" size="20" type="text"  placeholder="Enter Your Email" style="margin-bottom: 30px;"/><br>
   <div id="captchadiv">
        <div class="g-recaptcha" data-sitekey="site key" data-callback="verified"></div>
   </div>
   <button type="submit" value="Submit" id="reg_submit" style=" display:block;margin: 0 auto;"><img src="/favicon.png" style="width: 20px;float: left;" />Sign in</button>                  
 </form>
reCAPTCHA=require('recaptcha2')

        recaptcha=new reCAPTCHA({
            siteKey:'site key',
            secretKey:'secretKey'
        })
function validateform(){
    var captcha_response = grecaptcha.getResponse();
        if(captcha_response.length == 0 || grecaptcha != undefined )
        {
            // Captcha is not Passed
            return '  Please verify you are not a robot.';
        }else{
            $.get('/captchaTest',{'response':captcha_response},function(response){
                if(response == undefined && response.responseCode == undefined && response.responseDesc == undefined  && response.responseCode !== 0 && response.responseDesc !== 'Sucess' ){
                    return ' You are a robot.';
                }
                grecaptcha.reset();
            });
        }
}
app.get('/captchaTest',function(req,res){
    var requestQuery = req.query;
    if( requestQuery != undefined && requestQuery != '' && requestQuery != null && requestQuery.response != undefined && requestQuery.response != '' && requestQuery.response != null ){
        var response = requestQuery.response;
            var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret="+ secret_key +"&response=" +response;
            // Hitting GET request to the URL, Google will respond with success or error scenario.
            request(verificationUrl,function(error,response,body) {
            body = JSON.parse(body);
            // Success will be true or false depending upon captcha validation.
                if(body.success !== undefined && !body.success) {
                    res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
                }else{
                    res.send({"responseCode" : 0,"responseDesc" : "Sucess"});
                }
            });
    }else{
        res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
    }
});
我正在处理
节点js
。我正在使用谷歌
recaptcha2
,当我看到很多示例和所有示例时,使用表单提交验证
recaptcha
。它们在操作中定义,但我的操作方法在其他导航中使用,因此我可以使用
get,post
request。我不知道如何使用
get,post
请求
recaptcha
。我想使用
get,post
请求在服务器端验证
recaptcha

我需要后端验证工作的帮助。 提前谢谢

请尝试此代码

客户端代码不太安全,请同时使用客户端代码

客户端

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
   var verified = function() {
       document.getElementById("loginform").submit();
   };
</script>    

<form action="www.example.com/" method="POST" id="loginform" onsubmit=" return validation()">
   <input  id="email" maxlength="80" name="email" size="20" type="text"  placeholder="Enter Your Email" style="margin-bottom: 30px;"/><br>
   <div id="captchadiv">
        <div class="g-recaptcha" data-sitekey="site key" data-callback="verified"></div>
   </div>
   <button type="submit" value="Submit" id="reg_submit" style=" display:block;margin: 0 auto;"><img src="/favicon.png" style="width: 20px;float: left;" />Sign in</button>                  
 </form>
reCAPTCHA=require('recaptcha2')

        recaptcha=new reCAPTCHA({
            siteKey:'site key',
            secretKey:'secretKey'
        })
function validateform(){
    var captcha_response = grecaptcha.getResponse();
        if(captcha_response.length == 0 || grecaptcha != undefined )
        {
            // Captcha is not Passed
            return '  Please verify you are not a robot.';
        }else{
            $.get('/captchaTest',{'response':captcha_response},function(response){
                if(response == undefined && response.responseCode == undefined && response.responseDesc == undefined  && response.responseCode !== 0 && response.responseDesc !== 'Sucess' ){
                    return ' You are a robot.';
                }
                grecaptcha.reset();
            });
        }
}
app.get('/captchaTest',function(req,res){
    var requestQuery = req.query;
    if( requestQuery != undefined && requestQuery != '' && requestQuery != null && requestQuery.response != undefined && requestQuery.response != '' && requestQuery.response != null ){
        var response = requestQuery.response;
            var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret="+ secret_key +"&response=" +response;
            // Hitting GET request to the URL, Google will respond with success or error scenario.
            request(verificationUrl,function(error,response,body) {
            body = JSON.parse(body);
            // Success will be true or false depending upon captcha validation.
                if(body.success !== undefined && !body.success) {
                    res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
                }else{
                    res.send({"responseCode" : 0,"responseDesc" : "Sucess"});
                }
            });
    }else{
        res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
    }
});
服务器端

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
   var verified = function() {
       document.getElementById("loginform").submit();
   };
</script>    

<form action="www.example.com/" method="POST" id="loginform" onsubmit=" return validation()">
   <input  id="email" maxlength="80" name="email" size="20" type="text"  placeholder="Enter Your Email" style="margin-bottom: 30px;"/><br>
   <div id="captchadiv">
        <div class="g-recaptcha" data-sitekey="site key" data-callback="verified"></div>
   </div>
   <button type="submit" value="Submit" id="reg_submit" style=" display:block;margin: 0 auto;"><img src="/favicon.png" style="width: 20px;float: left;" />Sign in</button>                  
 </form>
reCAPTCHA=require('recaptcha2')

        recaptcha=new reCAPTCHA({
            siteKey:'site key',
            secretKey:'secretKey'
        })
function validateform(){
    var captcha_response = grecaptcha.getResponse();
        if(captcha_response.length == 0 || grecaptcha != undefined )
        {
            // Captcha is not Passed
            return '  Please verify you are not a robot.';
        }else{
            $.get('/captchaTest',{'response':captcha_response},function(response){
                if(response == undefined && response.responseCode == undefined && response.responseDesc == undefined  && response.responseCode !== 0 && response.responseDesc !== 'Sucess' ){
                    return ' You are a robot.';
                }
                grecaptcha.reset();
            });
        }
}
app.get('/captchaTest',function(req,res){
    var requestQuery = req.query;
    if( requestQuery != undefined && requestQuery != '' && requestQuery != null && requestQuery.response != undefined && requestQuery.response != '' && requestQuery.response != null ){
        var response = requestQuery.response;
            var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret="+ secret_key +"&response=" +response;
            // Hitting GET request to the URL, Google will respond with success or error scenario.
            request(verificationUrl,function(error,response,body) {
            body = JSON.parse(body);
            // Success will be true or false depending upon captcha validation.
                if(body.success !== undefined && !body.success) {
                    res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
                }else{
                    res.send({"responseCode" : 0,"responseDesc" : "Sucess"});
                }
            });
    }else{
        res.send({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
    }
});

是的,这是可行的,但如果您对recaptcha的节点模块有想法,请帮助我,因为节点模块更安全。我将尝试到现在为止,我没有在recaptcha节点模块上工作