Created
December 9, 2014 17:16
-
-
Save jaxbot/1ef9ecb72bd0df1d4f24 to your computer and use it in GitHub Desktop.
Node.js recaptcha example 1 https://jaxbot.me/articles/new-nocaptcha-recaptcha-with-node-js-express-12-9-2014
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<script src='https://www.google.com/recaptcha/api.js'></script> | |
</head> | |
<body> | |
<h1>Register for test site</h1> | |
<form action="register" method="post"> | |
<input type="text" name="username" placeholder="Username"><br> | |
<input type="text" name="email" placeholder="Email"><br> | |
<input type="password" name="password" placeholder="Password"><br> | |
<div class="g-recaptcha" data-sitekey="sitekey here"></div> | |
<input type="submit" value="Register"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var engines = require('consolidate'); | |
var app = express(); | |
var https = require('https'); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.engine('html', engines.hogan); | |
app.get('/', function(req, res) { | |
res.render('form.html'); | |
}); | |
app.post('/register', function(req, res) { | |
verifyRecaptcha(req.body["g-recaptcha-response"], function(success) { | |
if (success) { | |
res.end("Success!"); | |
// TODO: do registration using params in req.body | |
} else { | |
res.end("Captcha failed, sorry."); | |
// TODO: take them back to the previous page | |
// and for the love of everyone, restore their inputs | |
} | |
}); | |
}); | |
app.listen(3000); | |
var SECRET = "secret from webpage here"; | |
// Helper function to make API call to recatpcha and check response | |
function verifyRecaptcha(key, callback) { | |
https.get("https://www.google.com/recaptcha/api/siteverify?secret=" + SECRET + "&response=" + key, function(res) { | |
var data = ""; | |
res.on('data', function (chunk) { | |
data += chunk.toString(); | |
}); | |
res.on('end', function() { | |
try { | |
var parsedData = JSON.parse(data); | |
callback(parsedData.success); | |
} catch (e) { | |
callback(false); | |
} | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nodejs-recaptcha", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"consolidate": "^0.10.0", | |
"express": "^4.10.4", | |
"hogan.js": "^3.0.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here you can find the complete demo. Go through the link:
https://jsonworld.com/demo/45/nodejs/Implementing-google-reCAPTHCA-in--node.js-application