I'm trying to setup google recaptcha using this tutorial (https://codeforgeek.com/2016/03/google-recaptcha-node-js-tutorial/) and move the recaptcha code into it's own module. I get:
TypeError: res.json is not a function
in the console when I try this code:
var checkRecaptcha = function(req, res){
// g-recaptcha-response is the key that browser will generate upon form submit.
// if its blank or null means user has not selected the captcha, so return the error.
if(req.body['g-recaptcha-response'] === undefined || req.body['g-recaptcha-response'] === '' || req.body['g-recaptcha-response'] === null) {
return res.json({"responseCode" : 1,"responseDesc" : "Please select captcha"});
}
// Put your secret key here.
var secretKey = "************";
// req.connection.remoteAddress will provide IP address of connected user.
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + req.body['g-recaptcha-response'] + "&remoteip=" + req.connection.remoteAddress;
// Hitting GET request to the URL, Google will respond with success or error scenario.
var request = require('request');
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) {
return res.json({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
}
return res.json({"responseCode" : 0,"responseDesc" : "Sucess"});
});
}
module.exports = {checkRecaptcha};
Why does this happen? I do have app.use(bodyParser.json()); set in my app.js and res.json() seems to work fine in other parts of my app, just not this recaptcha module.
bodyParser.json()is for parsing JSON requests, not sending JSON responses)