validation码为nodejs创build

我正尝试在网站的注册页面上放置一个validation码。 我怎样才能在node.js中显示validation码?

你可以使用nodejs-recaptcha 。

这里是我的例子最后用Express框架处理ajax postrequest来显示由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); }); 

我发现了一个用纯js写的聪明的东西:

captchapng

特征

  • 只生成数字validation码PNG图像
  • 内置字体
  • 人物上下左右极限随机位移
  • 完整的JavaScript

它会产生像下面这样的png:

在这里输入图像描述

这是我的代码:

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}); }; 

有nodejs-recaptcha ,但我不知道它是多么成熟。

简单的validation码实现

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

    Interesting Posts