n-captcha

0.1.5 • Public • Published

n-captcha

nodejs captcha

How to install

  • brew install pkg-config if you don't have

  • brew install cairo if you don't have

  • npm install n-captcha --save

Config

{
  //verify code length
  textLen: 4,
 
  //verify code color
  color: '#2897CE',
 
  //verify code bg-color
  background: '#DFDFDF',
 
  lineWidth: 3,
  fontSize: 3,
  imageW: 200,
  imageH: 80
 
  //identify level
  level: 3,
 
  //bezier Curve color
  bcColor: '#97BFB2',
  beforReqEndHooks: function(){}
}

How to use

if you use express or koa you can use it as middleware

You server code

var app = express()();
var ncaptcha = require('n-captcha');
app.use(function(req, res, next) {
  if (req.url === '/captcha') {
    var options = {
      beforReqEndHooks: function(verifyCode) {
        if (req.session) req.session.ncaptcha = verifyCode;
        //do something you need
      }
    }
    var verifyCode = ncaptcha(options, res);
  } else {
    next();
  }
})
 
// route
 
app.post('/p/doLogin', function() {
  var ncaptcha = req.body.ncaptcha;
  if (ncaptcha !== req.session.ncaptcha) {
    res.json({code: 1000, msg: 'make sure that captcha is right'})
  }
})
 

You client code

<html>
 
<body>
  <img src="/captcha">
</body>
 
</html>
 

license

MIT

Dependents (0)

Package Sidebar

Install

npm i n-captcha

Weekly Downloads

2

Version

0.1.5

License

MIT

Last publish

Collaborators

  • progmer