Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
recaptcha验证码中间件
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
.jshintrc
.travis.yml
MIT-License
Makefile
README.md
package.json

README.md

re-captcha Build Status

recaptcha renders and verifies Recaptcha captchas.

Installation

Via npm:

npm install re-captcha

Setup

Before you can use this module, you must visit http://www.google.com/recaptcha to request a public and private API key for your domain.

Customizing the Recaptcha

See these instructions for help customizing the look of Recaptcha. In brief, you will need to add a structure like the following before the form in your document:

<script>
  var RecaptchaOptions = {
   theme: 'clean',
   lang: 'en'
  };
</script>

Example Using Connect/Express

app.js:

var express  = require('connect');
var Recaptcha = require('re-captcha');

var PUBLIC_KEY  = 'YOUR_PUBLIC_KEY';
var PRIVATE_KEY = 'YOUR_PRIVATE_KEY';
var recaptcha = new Recaptcha(PUBLIC_KEY, PRIVATE_KEY);

var app = connect();
app.use(connect.bodyParser());
app.get('/', function(req, res) {
  res.render('index', {
    layout: false,
    locals: {
      recaptcha_form: recaptcha.toHTML()
    }
  });
});

app.post('/', function(req, res) {
  var data = {
    remoteip:  req.connection.remoteAddress,
    challenge: req.body.recaptcha_challenge_field,
    response:  req.body.recaptcha_response_field
  };

  recaptcha.verify(data, function(err) {
    if (err) {
      // Redisplay the form.
      res.render('form.html', {
        layout: false,
        locals: {
          recaptcha_form: recaptcha.toHTML(err)
        }
      });
    } else {
      res.send('Recaptcha response valid.');
    }
  });
});

http.createServer(app).listen(3000);

views/form.html:

<form method="POST" action=".">
  <%-recaptcha_form%>
  <input type="submit">
</form>

Make sure connect and EJS are installed, then:

$ node app.js
Something went wrong with that request. Please try again.