Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs - How to use node-captcha module?

I've installed the node-captcha nodejs module from the following link...

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

The example given on the website is as follows...

...
var captcha = require('captcha');
...
app.configure(function(){
...
    app.use(app.router);
    app.use(captcha('/captcha.jpg')); // url for captcha jpeg
    app.use(express.static(__dirname + '/public'));
...

The app object is used in express but I'm not using express.

I've been trying to get captcha to work by calling "captcha('/captcha.jpg')" within the "http.createServer" function but nothing is happening.

I'm really puzzled as to how I make use of this module.

like image 378
mesh Avatar asked Oct 22 '25 03:10

mesh


1 Answers

Managed to write up a script that uses the module.

Also req.session.captcha = text should be commented out in the module's captcha.js file otherwise an error will be thrown. Commenting out that code does mean it should be replaced with a line that will store the captcha text to the session.

var http = require('http'), url = require('url'), captcha = require('captcha');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});

    var path = url.parse(req.url).pathname;
    switch (path) {
    case '/':
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write('<img src="/captcha.jpg" />');
        res.end();
        break;
    case '/captcha.jpg':
        captcha('/captcha.jpg')(req, res, 'noCaptcha');
        break;
}
}).listen(8124, "127.0.0.1");
console.log('Server running at http://127.0.0.1:8124/');

noCaptcha = function() {
    console.log('No captcha available');
};
like image 139
mesh Avatar answered Oct 23 '25 18:10

mesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!