-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathserver.mjs
40 lines (35 loc) · 1.31 KB
/
server.mjs
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
import express, { json } from 'express';
import fetch from 'node-fetch';
import cors from 'cors'; // Import the cors middleware
const captchaSecret = process.env.REACT_APP_CAPTCHA_S;
const app = express();
const port = 3000; // Choose a port for your server
app.use(json());
app.use(cors()); // Enable CORS for all routes
app.use(express.urlencoded({ extended: false }));
app.post('/verify-recaptcha', async (req, res) => {
const { token } = req.body;
const secret = captchaSecret; // Replace with your own reCAPTCHA secret key
const uri = `https://www.google.com/recaptcha/api/siteverify?secret=${secret}&response=${token}`;
fetch(uri, {
method: "post",
})
.then((response) => response.json())
.then((google_response) => {
// google as a response
if (google_response.success === true) {
// if captcha is verified
return res.send({ response: "Successful" });
} else {
// if captcha is not verified
return res.send({ response: "Failed" });
}
})
.catch((error) => {
// Some error while verify captcha
return res.json({ error });
});
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});