-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
46 lines (40 loc) · 1.42 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : false}));
app.get('/',function(req,res) {
// Sending our HTML file to browser.
res.sendFile(__dirname + '/index.html');
});
app.post('/submit',function(req,res){
// Put your secret key here.
var secretKey = "SECRET";
var userIpAddress = req.connection.remoteAddress;
var repsonseToken = req.body['test_token']
// req.connection.remoteAddress will provide IP address of connected user.
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify";
// Hitting GET request to the URL, Google will respond with success or error scenario.
request.post(verificationUrl, {
form: {
secret: secretKey,
response: repsonseToken,
remoteip: userIpAddress,
}
}, function(error,response,body) {
body = JSON.parse(body);
console.log("RESPONSE", body)
// Success will be true or false depending upon captcha validation.
if(body.success !== undefined && !body.success) {
return res.json({"responseCode" : 1,"responseDesc" : "Failed captcha verification"});
}
res.json({"responseCode" : 0,"responseDesc" : "Sucess"});
});
});
// This will handle 404 requests.
app.use("*",function(req,res) {
res.status(404).send("404");
})
// lifting the app on port 3000.
app.listen(3000);