生成验证码
构建简单的node服务器
- 新建serve文件夹
- 进入 serve文件夹初始化npm项目 npm init
- 新建server.js文件
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app);
const svgCaptcha = require('svg-captcha'); //验证码插件
//设置允许跨域访问
app.all("*", function(req, res, next) {
//设置允许跨域的域名,*代表允许任意域名跨域
res.header("Access-Control-Allow-Origin", "*");
//允许的header类型
res.header("Access-Control-Allow-Headers", "content-type");
//跨域允许的请求方式
res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
if (req.method.toLowerCase() == 'options')
res.send(200); //让options尝试请求快速结束
else
next();
})
// 获取验证码接口
app.get('/getCodeImg', (req, res) => {
// 字母验数字证码
// let codeConfig = {
// size: 4,// 验证码长度
// ignoreChars: '0o1i', // 验证码字符中排除 0o1i
// noise: 5, // 干扰线条的数量
// width: 100, //验证码宽度
// height: 40, //验证码高度
// fontSize: 40, //字体大小
// color: true, //开启字体颜色
// background: '#cc9966',//背景颜色
// // charPreset:'abcd123456789', //随机预设字符
// }
// let captcha = svgCaptcha.create(codeConfig); //验证码
// 数学表算式验证码
let codeConfig = {
mathMin: 1, //数学表达式的最小值
mathMax: 20, // 数学表达式的最大值
mathOperator: '+', // 使用的运算符:+、-或+-(用于随机的+或-)
noise: 5, // 干扰线条的数量
width: 100, //验证码宽度
height: 40, //验证码高度
fontSize: 40, //字体大小
color: