node 使用 svg-captvcha 模块
主要代码
- // 登录验证码
- app.get('/api/captcha',
- function(req, res) {
- var captcha = svgCaptcha.create();
- // req.session.captcha = captcha.text;
- res.type('application/json');
- res.status(200).send({
- svg: captcha.data,
- answer: captcha.text,
- });
- });
react 前端显示
- getvertification() {
- fetch('/api/captcha', {
- method: 'GET',
- headers: {
- 'Content-Type': 'application/json'
- }
- }).then(data = >{
- // tslint:disable-next-line:no-console
- return data.json();
- }).then(mjson = >{
- // tslint:disable-next-line:no-console
- console.log(mjson);
- this.setState({
- verification_data: mjson.svg,
- answer: mjson.answer
- });
- setTimeout(() = >{
- if (this.refs.captcha_img) { (this.refs.captcha_img as htmlDivElement).innerHTML = mjson.svg;
- }
- },
- 100);
- // if(verification === ansewer)
- });
- }
来源: https://www.2cto.com/kf/201803/727002.html