const express = require('express')
const session = require('express-session')
const bodyParser = require('body-parser')
const expressCaptcha = require('express-svg-captcha')
const app = express()
const captcha = new expressCaptcha({
isMath: true,
useFont: null,
size: 4,
ignoreChars: '0o1i',
noise: 3,
color: true,
background: null,
width: 150,
height: 50,
fontSize: 56,
charPreset: null,
})
app.use(session({
secret: 'your secret',
resave: false,
saveUninitialized: true,
}))
app.use(bodyParser.urlencoded({
extended: false
}))
app.get('/captcha', captcha.generate())
app.get('/', function (req, res, next) {
res.type('html')
res.end(`
<img src="/captcha"/>
<form action="/test" method="post">
<input type="text" name="captcha"/>
<input type="submit"/>
</form>
`)
})
app.post('/test', (req, res) => {
res.type('html')
res.end(`
<p>CAPTCHA VALID: ${captcha.validate(req, req.body.captcha)}</p>
`)
})
app.listen(80, () => {
console.log('server started')
})