Reputation: 289
I am trying to place a captcha on the registration page of a website. How can I show a captcha in node.js?
Upvotes: 13
Views: 26774
Reputation: 317
I know this question is quite old, but I recently made my own implementation of captcha, and it may help anyone struggling with the topic.
check out vanilla-captcha as the name indicates, is made in pure JS, is meant to run in the server as well as in the browser, there is a demo in case you want to give it a go. is very simple to use and customize.
Upvotes: 2
Reputation: 146
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
Upvotes: 2
Reputation: 3723
I have found one which is smart written with pure js:
Features
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});
};
Upvotes: 8
Reputation: 175
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);
});
Upvotes: 4
Reputation: 27342
There is nodejs-recaptcha, but I don't know how mature it is.
Upvotes: 2