npm install captchaSimple captcha for Express.
WARNING! New API (0.0.5 -> 0.1.0)
Via npm:
$ npm install captcha
``javascript
'use strict'
const express = require('express')
const session = require('express-session')
const bodyParser = require('body-parser')
const captchaUrl = '/captcha.jpg'
const captchaId = 'captcha'
const captchaFieldName = 'captcha'
const captcha = require('./captcha').create({ cookie: captchaId })
const app = express()
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
}))
app.use(bodyParser.urlencoded({ extended: false }))
app.get(captchaUrl, captcha.image())
app.get('/', (req, res) => {
res.type('html')
res.end(
)
})app.post('/login', (req, res) => {
res.type('html')
res.end(
CAPTCHA VALID: ${ captcha.check(req, req.body[captchaFieldName]) }
)
})app.listen(8080, () => {
console.log('server started')
})
``