站内搜索:
首页 >> 前端 >> 内容
怎么利用node和react做验证码功能?

时间:2018/3/8 14:17:28

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)
        });
    }

  • 上一篇:chrome浏览器如何隐藏inputtextarea获取焦点后边框?
  • 下一篇:web前端之css美化操作实例
  • 返回顶部