NodeJs 生成验证码
NodeJs生成验证码还真是不好弄呀。有些是要用到C++内库什么的,在安装依赖的时候安装的东西很多,在跨平方便还真是非常的不方便呢
今天在网上找一个用svg生成的验证码,这个真心不错,标记一下。
安装方式如下 svg-captcha
npm install --save svg-captcha
安装好了,调用方法如:
- 返回的是一个字符串,前端调用的时候,前端调用把返回的数据按svg的方法展示出来就可以了。
get_captcha(req, res, options) {
const captcha = vCode.create({ fontSize: 50, width: 100 });
res.Send(captcha.data);
}
- 下面这段代码是返回的是一个 url, 以url的形式展示出来,这个更方便些
get_captchaCode(req, res, options) {
const captcha = vCode.create({ fontSize: 50, width: 100, height: 40 });
res.setHeader('Content-Type', 'image/svg+xml');
res.write(String(data));
res.end();
}
在页面用一个img展示出来就可以了。
<img _ngcontent-c3="" src="https://127.0.0.1:30081/webapi/apihelper/captchaCode?times1502639056067">