node生成验证码
前言:
使用第三方库 npm install --save svg-captcha
效果1:
效果2:
代码
const express = require('express'); const router = express.Router(); const svgCaptcha = require('svg-captcha'); // 直接是字母的 router.get('/', (req, res, next) => { const captcha = svgCaptcha.create({ size: 6, ignoreChars: '0o1liO', noise: '3', color: true, background: '#c67728' }); req.session.captcha = captcha.text; res.type('svg'); res.status(200).send(captcha.data); }) // 数字计算的 router.get('/calculate', (req, res, next) => { const captcha = svgCaptcha.createMathExpr({ size: 6, ignoreChars: '0o1liO', noise: '3', color: true, background: '#c67728' }); req.session.captcha = captcha.text; res.type('svg'); res.status(200).send(captcha.data); }) module.exports = router;