http-mw-captcha
v1.1.0
Published
Simple & elegant captcha extension for http-mw module
Downloads
2
Maintainers
Readme
http-mw-captcha
Simple & elegant captcha extension for http-mw
module. Takes advantage of self initiated session feature of it. Not a cutting edge technology but provides basic protection in two lines without relying on third party services.
Features
- Doesn't rely on third party services.
- Easy to implement
- Random backgrounds
- Random text pos and direction
- Random fonts
Installation
$ npm i http-mw-captcha
It automatically installs http-mw module as a dependency if you don't have it yet.
Usage
const server = require("http-mw");
const captcha = require("http-mw-captcha");
server.listen({
"port": 8080,
"webroot": "./public",
"uploads": "./files"
}, function (req, res) {
switch (req.method) {
case "GET":
switch (req.path[0]) {
case "captcha":
captcha.generate(req, function (data) {
res.writeHead(200, {'Content-Type': server.mime("png")});
res.end(data);
});
}
break;
case "POST":
switch (req.path[0]) {
case "verify":
if (captcha.verify(req, req.payload["code"]) === true) {
req.send("code verified");
} else {
req.send("invalid code");
}
break;
default:
req.deny("undefined endpoint", 404);
}
}
}, function (conf) {
console.log("-- server running on port " + conf.port);
console.log("-- test url: http://localhost:" + conf.port + "/captcha");
});
Documentation
captcha.generate= function(req,cb); // takes request object and returns raw png buffer as callback captcha.verify(code,req); // takes request object and returns true or false