node生成驗證碼
阿新 • • 發佈:2021-01-01
技術標籤:node
node生成驗證碼
前言:
使用第三方庫 npm install --savesvg-captcha
svg驗證碼
效果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;