Creating captcha for nodejs

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

+4
source share
4 answers

You can use nodejs-recaptcha .

Here is my example, which uses it in conclusion, when the Express environment processes ajax postrequest to display contact information protected by 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); }); 
+6
source

I found one that is smart written with pure js:

captchapng

Functions

  • Only generate numeric PNG image with conversion
  • Embedded fonts
  • Characters up and down, left and right limits, random movement.
  • Full javascript

It will generate png like:

enter image description here

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}); }; 
+5
source

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

+2
source

simple captcha implementation

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

-1
source

Source: https://habr.com/ru/post/1381249/


All Articles