captcha creation for nodejs

淺唱寂寞╮ 提交于 2019-11-29 10:34:47

问题


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


回答1:


I have found one which is smart written with pure js:

captchapng

Features

  • Only generate numeric captcha PNG image
  • Build-in fonts
  • Characters up and down, left and right limits, random displacement
  • Full JavaScript

It will generate png like :

And here is my code :

ejs -[express3.x]

<img src="data:image/jpeg;base64,<%= valicode %>"/>

js

var captchaImg = function(){
        var p = new captchapng(80,30,parseInt(Math.random()*9000+1000)); // width,height,numeric captcha
        p.color(115, 95, 197, 100);  // First color: background (red, green, blue, alpha)
        p.color(30, 104, 21, 255); // Second color: paint (red, green, blue, alpha)
        var img = p.getBase64();
        var imgbase64 = new Buffer(img,'base64');
        return imgbase64;
} 

exports.index_get = function(req, res){
   var valicode = new Buffer(captchaImg()).toString('base64');       
   res.render('index', {'valicode' : valicode});
};



回答2:


You could use nodejs-recaptcha.

Here is my example using it in conclusion with the Express framework handling an ajax postrequest to display contact data protected by a reCaptcha.

app.post('/ajax/contact/', function(req, res, next) {
        var recaptcha = new recaptcha_async.reCaptcha();

        // Eventhandler that is triggered by checkAnswer()
        recaptcha.on('data', function (recaptcha_response) {
                res.render('contact', {
                        layout: 'contact_layout.json.ejs',
                        locals: {
                                recaptcha: recaptcha_response.is_valid ? 'valid' : 'invalid'
                                }
                });
        });

        // Check the user response by calling the google servers
        // and sends a 'data'-event
        recaptcha.checkAnswer('aLfsZvFVbAbAbzsxlnHbH7wxx0PbNbGabHXbpZgl',  // private reCaptchakey (invalidated)
                          req.connection.remoteAddress,
                          req.body.recaptcha_challenge_field,
                          req.body.recaptcha_response_field);
});



回答3:


There is nodejs-recaptcha, but I don't know how mature it is.




回答4:


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




回答5:


simple captcha implementation

https://github.com/napa3um/node-captcha



来源:https://stackoverflow.com/questions/8132531/captcha-creation-for-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!