不依赖canvas的Node.js验证码模块captchapng
发布时间:2020-12-13 22:14:35 所属栏目:百科 来源:网络整理
导读:http://itbilu.com/nodejs/npm/N1wjsIxP.html 试过很多个验证码模块,要么依赖canvas,要么需要编译,使用都不够简单方便。captchapng是一个基于pnglib模块开发,数字型验证码模块。内置字体、全JavaScript无其它依赖,使用非常简单高效,很符合我的使用需求
http://itbilu.com/nodejs/npm/N1wjsIxP.html
1. 模块安装npminstallcaptchapng--save 2.使用示例varhttp=require('http'); varcaptchapng=require('captchapng'); http.createServer(function(request,response){ if(request.url=='/captcha.png'){ varp=newcaptchapng(80,30,parseInt(Math.random()*9000+1000));//宽,高,数字验证码 p.color(0,0);//Firstcolor:background(red,green,blue,alpha) p.color(80,80,255);//Secondcolor:paint(red,alpha) varimg=p.getBase64(); varimgbase64=newBuffer(img,'base64'); response.writeHead(200,{ 'Content-Type':'image/png' }); response.end(imgbase64); }elseresponse.end(''); }).listen(8181); console.log('Webserverstarted.nhttp:127.0.0.1:8181captcha.png'); 3.在Express框架中使用为了能在项目不同位置使用,我在方法中增加了query参数自定义宽高的设置。示例如下: varcaptchapng=require('captchapng'); exports.captchap=function(req,res,next){ varwidth=!isNaN(parseInt(req.query.width))?parseInt(req.query.width):100; varheight=!isNaN(parseInt(req.query.height))?parseInt(req.query.height):30; varcode=parseInt(Math.random()*9000+1000); req.session.checkcode=code; varp=newcaptchapng(width,height,code); p.color(0,0); p.color(80,255); varimg=p.getBase64(); varimgbase64=newBuffer(img,'base64'); res.writeHead(200,{ 'Content-Type':'image/png' }); res.end(imgbase64); } 在前端页面,调用方法对应路由即可。 <imgsrc="/checkcode?width=100&height=30"/> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |