-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (36 loc) · 1.32 KB
/
server.js
File metadata and controls
45 lines (36 loc) · 1.32 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const fetch = require('node-fetch'); // npm install node-fetch
const app = express();
const PORT = 3000;
// Replace with your secret key
const RECAPTCHA_SECRET = '6Le0YeQrAAAAADydYsw8NoeIvfqMDfHyI8_5AFBE';
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));
// Serve captcha page as the landing page
app.get('/', (req, res) => {
res.sendFile(__dirname + 'captcha.html');
});
// Handle form submission from captcha.html
app.post('/verify', async (req, res) => {
const token = req.body['g-recaptcha-response'];
if(!token){
return res.send('CAPTCHA not completed. <a href="/">Try again</a>');
}
const verificationUrl = `https://www.google.com/recaptcha/api/siteverify?secret=${RECAPTCHA_SECRET}&response=${token}`;
try {
const response = await fetch(verificationUrl, { method: 'POST' });
const data = await response.json();
if(data.success){
// CAPTCHA passed → redirect to index.html
res.redirect('/index.html');
} else {
res.send('CAPTCHA verification failed. <a href="/">Try again</a>');
}
} catch (err) {
res.send('Error verifying CAPTCHA. <a href="/">Try again</a>');
}
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});