Git Product home page Git Product logo

gt-node-sdk's Introduction

#Update in 0.3.3 更新内容 You can modify the api server address

你现在可以修改APIServer的地址了

Update in 0.3.1 更新内容

Use register api to check the server status, if it return false, switch to local captcha manually

现在可以用register接口作为检查服务器是否正常的方法,如果register返回false则切换为本地验证码

What's New in 0.2.0 更新内容

Pass public key to use register API, optional right now, if you don't use it now, the captcha might broke some time in the future

将你的Public Key作为第二个参数传入,此参数现在为可选参数,只有传入才能够使用register接口。Register接口可能在将来成为必须的接口

#Install 安装

npm install geetest

#Setup 配置

PRIVATE_KEY is the key, PUBLIC_KEY is the ID

var geetest = require('geetest')('YOUR_PRIVATE_KEY', ['YOUR_PUBLIC_KEY');

#Usage 使用流程

###1.Init with private key and public key 使用私钥和公钥初始化

var geetest = require('geetest)('Private key', 'Public Key'[, 'api server'])

If you are authorized to modify api server, pass the api server as third parameter, it must ends with '/'
如果是可以自定义api地址的用户,可以将api地址作为第三个参数传入,注意要以'/'结尾,例如'http://api.geetest.com/'

###2.Use register api to get challenge on each request 在每次用户请求验证码时使用register接口获取challenge

geetest.register(function(challenge) {
	if(challenge) {
		//deal with it
		res.json({challenge: challenge})
	}
})

###3.Add captcha script to your page 在页面上添加验证的script

Put the challenge into the src 将challenge作为参数传入src

$http.get('/request/to/your/register/api').success(function(e) {
	var s = document.createElement('script');
	s.src = 'http://api.geetest.com/get.php?gt=yourPublicKey&challenge=' + challenge;
	s.async = true;
	document.body.append(s);//append the script where ever you want
})

###4.Validate the result 验证前端的提交

See Validate API or Express Middleware below
见以下Validate接口或Express中间件 #Node.js Validate API 验证函数

geetest.validate({
	challenge: //form's [geetest_challenge],
	validate: //form's [geetest_validate],
	seccode: //form's [geetest_seccode],
}, function(result) {
	if(result) {
		//validate pass
	}
	else {
		//validate fail
	}
})

#Node.js Register API 验证函数

geetest.register(function(challenge) {
	if(challenge) {
		//put this challenge into the request of get in your website
		//将challenge作为参数传入前端的get请求
	}
	else {
		//Fail to reach the server, use failback method
		//无法链接无服务器,使用本地验证码
	}
})

#Express Note: express.bodyParser() is required for geetest-sdk's bodyParser.

##Use as middleware 使用中间件

###Router 路由:

app.post('/someForm', geetest.bodyParser, yourHandler);

###API:

module.exports.yourHandler = function(req, res) {
	if(req.geetest) {
		console.log('GeeTest captcha validation pass');
		//DO WHAT EVER NEXT
		res.json({success: true})
	}
	else {
		res.json({err: 'GeeTest captcha validation fail'});
	}
}

gt-node-sdk's People

Contributors

suzukua avatar zhangcx93 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.