├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── index.html └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Shahid Shaikh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # google reCAPTCHA using Node.js 2 | 3 | ## How to run the code 4 | 5 | Clone the project. 6 | 7 | Switch to the cloned directory and run ```npm install``` to install required dependency. 8 | 9 | Run ```node app.js``` and visit ```localhost:3000``` to view the app. 10 | 11 | ## Tutorial 12 | 13 | https://codeforgeek.com/google-recaptcha-node-js-tutorial/ 14 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var bodyParser = require('body-parser'); 3 | var request = require('request'); 4 | var app = express(); 5 | 6 | app.use(bodyParser.json()); 7 | app.use(bodyParser.urlencoded({extended : false})); 8 | 9 | app.get('/',function(req,res) { 10 | // Sending our HTML file to browser. 11 | res.sendFile(__dirname + '/index.html'); 12 | }); 13 | 14 | app.post('/submit',function(req,res){ 15 | // g-recaptcha-response is the key that browser will generate upon form submit. 16 | // if its blank or null means user has not selected the captcha, so return the error. 17 | if(req.body['g-recaptcha-response'] === undefined || req.body['g-recaptcha-response'] === '' || req.body['g-recaptcha-response'] === null) { 18 | return res.json({"responseCode" : 1,"responseDesc" : "Please select captcha"}); 19 | } 20 | // Put your secret key here. 21 | var secretKey = "<>"; 22 | // req.connection.remoteAddress will provide IP address of connected user. 23 | var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + req.body['g-recaptcha-response'] + "&remoteip=" + req.connection.remoteAddress; 24 | // Hitting GET request to the URL, Google will respond with success or error scenario. 25 | request(verificationUrl,function(error,response,body) { 26 | body = JSON.parse(body); 27 | // Success will be true or false depending upon captcha validation. 28 | if(body.success !== undefined && !body.success) { 29 | return res.json({"responseCode" : 1,"responseDesc" : "Failed captcha verification"}); 30 | } 31 | res.json({"responseCode" : 0,"responseDesc" : "Sucess"}); 32 | }); 33 | }); 34 | 35 | // This will handle 404 requests. 36 | app.use("*",function(req,res) { 37 | res.status(404).send("404"); 38 | }) 39 | 40 | // lifting the app on port 3000. 41 | app.listen(3000); 42 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Google recapcha using Node.js demo - Codeforgeek 4 | 5 | 6 | 7 |

Google reCAPTHA Demo using Node.js

8 | Google recapcha demo - Codeforgeek 9 | 10 | 11 | 12 | 13 | 26 |
27 |

Google recaptcha using Node.js

28 |
29 |

30 |

31 |

32 |

33 |
34 |
35 | 36 | 37 | 38 | 39 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "google-racapcha-node", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/codeforgeek/google-racapcha-node.git" 12 | }, 13 | "author": "Shahid shaikh", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/codeforgeek/google-racapcha-node/issues" 17 | }, 18 | "homepage": "https://github.com/codeforgeek/google-racapcha-node#readme", 19 | "dependencies": { 20 | "body-parser": "^1.15.0", 21 | "express": "^4.13.4", 22 | "request": "^2.69.0" 23 | } 24 | } 25 | --------------------------------------------------------------------------------