-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (37 loc) · 1.07 KB
/
server.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
'use strict'
const express = require('express');
const app = express();
const session = require('express-session');
const bodyParser = require('body-parser');
const captchaConfig = require('./config');
const captcha = require('captcha').create(captchaConfig.params);
// configure session store
app.use(session({
secret: 'SECRETKEYWILLGOHERE',
resave: false,
saveUninitialized: true,
}));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.get(captchaConfig.captchaUrl, captcha.image());
// render captcha
app.get('/', (req, res) => {
res.type('html')
res.end(`
<img src="${captchaConfig.captchaUrl}"/>
<form action="/check" method="post">
<input type="text" name="captchaDigits"/>
<input type="submit"/>
</form>
`);
});
// validate captcha
app.post('/check', (req, res) => {
res.type('html')
res.end(`
<p>IS CAPTCHA VALID: ${ captcha.check(req, req.body.captchaDigits) }</p>
`)
});
app.listen(3000, () => {
console.log('Server started');
});