NodeJs 生成SVG图形验证码

使用svg-captcha插件在node.js中生成svg格式的验证码。

1、安装

npm install --save svg-captcha

2、在express中使用方法

// 导入svg-captcha模块
const svgCaptcha = require('svg-captcha');
//创建Router对象
const router = express.Router();

router.get('/captcha',function (req, resp) {
    const cap = svgCaptcha.create({size:4,noise:1,ignoreChars:'0o1i',background:'#DFE1CC'});
    //const cap = svgCaptcha.createMathExpr({mathMin: 1,mathMax:9,background:'#fffffd'});
    req.session.captcha = cap.text; // session 存储
    console.log(req.session.captcha);
    //console.log(cap.data);
    resp.type('svg'); // 响应的类型
    resp.send(cap.data);
});



//登录操作 http://localhost:8080/student/login
router.post("/login", function (req, resp) {
    //定义返回的对象
    let obj = {succ:false,msg:""};
    //判断验证码
    //从session获取图片验证码
    let yzm = req.session.captcha.toUpperCase();
    if(params.yzm.toUpperCase()==yzm){
        obj.msg="验证码正确";
        resp.json(obj);
    }else{
        obj.msg="验证码错误";
        resp.json(obj);
    }


});

 3、前端页面

function getcaptcha(){
            $("#yzmimg").attr("src","http://localhost:8080/student/captcha?r="+new Date().getTime());
        }


<img id="yzmimg" src="http://localhost:8080/student/captcha" width="100" height="34" onclick="getcaptcha()"/>

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值