Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

captcha creation for nodejs

I am trying to place a captcha on the registration page of a website. How can I show a captcha in node.js?

like image 555
Chris Avatar asked Nov 15 '11 06:11

Chris


1 Answers

Check out node-captcha-generator

It uses the MNIST Database to generate numerical captcha images. Pretty easy to integrate. I had used it on a previous website, it generates captcha images that look like this

Very simple usage as well. Here is an example for a GET request for generating a new Captcha image on each request (Express):

let Captcha = require('node-captcha-generator');

router.get('/imageGen', function(req, res, next) {
    var c = new Captcha({
        length:5, // Captcha length
        size:{    // output size
            width: 450,
            height: 200
        }
    });

    c.toBase64(function(err, base64){
        base64Data  =   base64.replace(/^data:image\/png;base64,/, "");
        base64Data  +=  base64Data.replace('+', ' ');
        console.log(base64Data);
        binaryData  =   new Buffer(base64Data, 'base64').toString('binary');
            if(err){
                console.log("Captcha Error");
                console.log(err);
            }
            else{
                res.contentType('image/png');
                res.end(binaryData,'binary');
            }
    });
});

Hope this answer helps, and unlike reCaptcha, no need for a HTTPS certificate for integrating in your website. Perfect for college/hobby projects

like image 185
Hemant H Kumar Avatar answered Sep 29 '22 22:09

Hemant H Kumar