0

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.

4
  • 1
    How are you using/including the module/middleware you've shown? (Also, bodyParser.json() is for parsing JSON requests, not sending JSON responses) Commented Mar 3, 2017 at 3:58
  • is there a specific line where you get the error? Commented Mar 3, 2017 at 4:03
  • @jonathanGB I get the error on line 7, 23, and 25 (which depends on the google recaptcha response). Commented Mar 3, 2017 at 7:28
  • @mscdex I'm using passportjs when the form is submitted. ` app.post('/login', function(req, res, next) { var recaptcha = require('./recaptcha'); recaptcha.checkRecaptcha(req, function(err, response) { //rest of the code` Commented Mar 3, 2017 at 7:30

1 Answer 1

1

Based on your usage of the middleware, you're not passing res to the function, but instead a callback (and checkRecaptcha() doesn't have a callback parameter since it responds directly to the request).

Try this instead:

app.post('/login', function(req, res) {
  var recaptcha = require('./recaptcha');
  recaptcha.checkRecaptcha(req, res);
});

or more simply:

app.post('/login', require('./recaptcha'));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.